16c3cef48STakashi Sakamoto /* 26c3cef48STakashi Sakamoto * motu.c - a part of driver for MOTU FireWire series 36c3cef48STakashi Sakamoto * 46c3cef48STakashi Sakamoto * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> 56c3cef48STakashi Sakamoto * 66c3cef48STakashi Sakamoto * Licensed under the terms of the GNU General Public License, version 2. 76c3cef48STakashi Sakamoto */ 86c3cef48STakashi Sakamoto 96c3cef48STakashi Sakamoto #include "motu.h" 106c3cef48STakashi Sakamoto 116c3cef48STakashi Sakamoto #define OUI_MOTU 0x0001f2 126c3cef48STakashi Sakamoto 136c3cef48STakashi Sakamoto MODULE_DESCRIPTION("MOTU FireWire driver"); 146c3cef48STakashi Sakamoto MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>"); 156c3cef48STakashi Sakamoto MODULE_LICENSE("GPL v2"); 166c3cef48STakashi Sakamoto 1759f6482cSTakashi Sakamoto const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = { 1859f6482cSTakashi Sakamoto /* mode 0 */ 1959f6482cSTakashi Sakamoto [0] = 44100, 2059f6482cSTakashi Sakamoto [1] = 48000, 2159f6482cSTakashi Sakamoto /* mode 1 */ 2259f6482cSTakashi Sakamoto [2] = 88200, 2359f6482cSTakashi Sakamoto [3] = 96000, 2459f6482cSTakashi Sakamoto /* mode 2 */ 2559f6482cSTakashi Sakamoto [4] = 176400, 2659f6482cSTakashi Sakamoto [5] = 192000, 2759f6482cSTakashi Sakamoto }; 2859f6482cSTakashi Sakamoto 296c3cef48STakashi Sakamoto static void name_card(struct snd_motu *motu) 306c3cef48STakashi Sakamoto { 316c3cef48STakashi Sakamoto struct fw_device *fw_dev = fw_parent_device(motu->unit); 326c3cef48STakashi Sakamoto struct fw_csr_iterator it; 336c3cef48STakashi Sakamoto int key, val; 346c3cef48STakashi Sakamoto u32 version = 0; 356c3cef48STakashi Sakamoto 366c3cef48STakashi Sakamoto fw_csr_iterator_init(&it, motu->unit->directory); 376c3cef48STakashi Sakamoto while (fw_csr_iterator_next(&it, &key, &val)) { 386c3cef48STakashi Sakamoto switch (key) { 396c3cef48STakashi Sakamoto case CSR_VERSION: 406c3cef48STakashi Sakamoto version = val; 416c3cef48STakashi Sakamoto break; 426c3cef48STakashi Sakamoto } 436c3cef48STakashi Sakamoto } 446c3cef48STakashi Sakamoto 456c3cef48STakashi Sakamoto strcpy(motu->card->driver, "FW-MOTU"); 465e03c33eSTakashi Sakamoto strcpy(motu->card->shortname, motu->spec->name); 475e03c33eSTakashi Sakamoto strcpy(motu->card->mixername, motu->spec->name); 486c3cef48STakashi Sakamoto snprintf(motu->card->longname, sizeof(motu->card->longname), 495e03c33eSTakashi Sakamoto "MOTU %s (version:%d), GUID %08x%08x at %s, S%d", 505e03c33eSTakashi Sakamoto motu->spec->name, version, 516c3cef48STakashi Sakamoto fw_dev->config_rom[3], fw_dev->config_rom[4], 526c3cef48STakashi Sakamoto dev_name(&motu->unit->device), 100 << fw_dev->max_speed); 536c3cef48STakashi Sakamoto } 546c3cef48STakashi Sakamoto 558865a31eSTakashi Sakamoto static void motu_card_free(struct snd_card *card) 568865a31eSTakashi Sakamoto { 57*3babca45STakashi Sakamoto struct snd_motu *motu = card->private_data; 58*3babca45STakashi Sakamoto 59*3babca45STakashi Sakamoto snd_motu_transaction_unregister(motu); 60*3babca45STakashi Sakamoto snd_motu_stream_destroy_duplex(motu); 618865a31eSTakashi Sakamoto } 628865a31eSTakashi Sakamoto 638865a31eSTakashi Sakamoto static void do_registration(struct work_struct *work) 648865a31eSTakashi Sakamoto { 658865a31eSTakashi Sakamoto struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work); 668865a31eSTakashi Sakamoto int err; 678865a31eSTakashi Sakamoto 688865a31eSTakashi Sakamoto if (motu->registered) 698865a31eSTakashi Sakamoto return; 708865a31eSTakashi Sakamoto 718865a31eSTakashi Sakamoto err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0, 728865a31eSTakashi Sakamoto &motu->card); 738865a31eSTakashi Sakamoto if (err < 0) 748865a31eSTakashi Sakamoto return; 75*3babca45STakashi Sakamoto motu->card->private_free = motu_card_free; 76*3babca45STakashi Sakamoto motu->card->private_data = motu; 778865a31eSTakashi Sakamoto 788865a31eSTakashi Sakamoto name_card(motu); 798865a31eSTakashi Sakamoto 802e76701bSTakashi Sakamoto err = snd_motu_transaction_register(motu); 812e76701bSTakashi Sakamoto if (err < 0) 822e76701bSTakashi Sakamoto goto error; 832e76701bSTakashi Sakamoto 849b2bb4f2STakashi Sakamoto err = snd_motu_stream_init_duplex(motu); 859b2bb4f2STakashi Sakamoto if (err < 0) 869b2bb4f2STakashi Sakamoto goto error; 879b2bb4f2STakashi Sakamoto 884638ec6eSTakashi Sakamoto snd_motu_proc_init(motu); 894638ec6eSTakashi Sakamoto 90dd49b2d1STakashi Sakamoto err = snd_motu_create_pcm_devices(motu); 91dd49b2d1STakashi Sakamoto if (err < 0) 92dd49b2d1STakashi Sakamoto goto error; 93dd49b2d1STakashi Sakamoto 948b460c76STakashi Sakamoto if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) || 958b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) || 968b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) || 978b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) { 989e796e7dSTakashi Sakamoto err = snd_motu_create_midi_devices(motu); 999e796e7dSTakashi Sakamoto if (err < 0) 1009e796e7dSTakashi Sakamoto goto error; 1019e796e7dSTakashi Sakamoto } 1029e796e7dSTakashi Sakamoto 10371c37977STakashi Sakamoto err = snd_motu_create_hwdep_device(motu); 10471c37977STakashi Sakamoto if (err < 0) 10571c37977STakashi Sakamoto goto error; 10671c37977STakashi Sakamoto 1078865a31eSTakashi Sakamoto err = snd_card_register(motu->card); 1088865a31eSTakashi Sakamoto if (err < 0) 1098865a31eSTakashi Sakamoto goto error; 1108865a31eSTakashi Sakamoto 1118865a31eSTakashi Sakamoto motu->registered = true; 1128865a31eSTakashi Sakamoto 1138865a31eSTakashi Sakamoto return; 1148865a31eSTakashi Sakamoto error: 1158865a31eSTakashi Sakamoto snd_card_free(motu->card); 1168865a31eSTakashi Sakamoto dev_info(&motu->unit->device, 1178865a31eSTakashi Sakamoto "Sound card registration failed: %d\n", err); 1186c3cef48STakashi Sakamoto } 1196c3cef48STakashi Sakamoto 1206c3cef48STakashi Sakamoto static int motu_probe(struct fw_unit *unit, 1216c3cef48STakashi Sakamoto const struct ieee1394_device_id *entry) 1226c3cef48STakashi Sakamoto { 1236c3cef48STakashi Sakamoto struct snd_motu *motu; 1246c3cef48STakashi Sakamoto 1258865a31eSTakashi Sakamoto /* Allocate this independently of sound card instance. */ 126366a20d7STakashi Sakamoto motu = devm_kzalloc(&unit->device, sizeof(struct snd_motu), GFP_KERNEL); 127366a20d7STakashi Sakamoto if (!motu) 1288865a31eSTakashi Sakamoto return -ENOMEM; 1296c3cef48STakashi Sakamoto motu->unit = fw_unit_get(unit); 1308865a31eSTakashi Sakamoto dev_set_drvdata(&unit->device, motu); 1316c3cef48STakashi Sakamoto 132366a20d7STakashi Sakamoto motu->spec = (const struct snd_motu_spec *)entry->driver_data; 1336c3cef48STakashi Sakamoto mutex_init(&motu->mutex); 1349e796e7dSTakashi Sakamoto spin_lock_init(&motu->lock); 13571c37977STakashi Sakamoto init_waitqueue_head(&motu->hwdep_wait); 1366c3cef48STakashi Sakamoto 1378865a31eSTakashi Sakamoto /* Allocate and register this sound card later. */ 1388865a31eSTakashi Sakamoto INIT_DEFERRABLE_WORK(&motu->dwork, do_registration); 1398865a31eSTakashi Sakamoto snd_fw_schedule_registration(unit, &motu->dwork); 1406c3cef48STakashi Sakamoto 1416c3cef48STakashi Sakamoto return 0; 1426c3cef48STakashi Sakamoto } 1436c3cef48STakashi Sakamoto 1446c3cef48STakashi Sakamoto static void motu_remove(struct fw_unit *unit) 1456c3cef48STakashi Sakamoto { 1466c3cef48STakashi Sakamoto struct snd_motu *motu = dev_get_drvdata(&unit->device); 1476c3cef48STakashi Sakamoto 1488865a31eSTakashi Sakamoto /* 1498865a31eSTakashi Sakamoto * Confirm to stop the work for registration before the sound card is 1508865a31eSTakashi Sakamoto * going to be released. The work is not scheduled again because bus 1518865a31eSTakashi Sakamoto * reset handler is not called anymore. 1528865a31eSTakashi Sakamoto */ 1538865a31eSTakashi Sakamoto cancel_delayed_work_sync(&motu->dwork); 1548865a31eSTakashi Sakamoto 1558865a31eSTakashi Sakamoto if (motu->registered) { 15661ccc6f6STakashi Sakamoto // Block till all of ALSA character devices are released. 15761ccc6f6STakashi Sakamoto snd_card_free(motu->card); 1588865a31eSTakashi Sakamoto } 1595b14ec25STakashi Sakamoto 1605b14ec25STakashi Sakamoto mutex_destroy(&motu->mutex); 1615b14ec25STakashi Sakamoto fw_unit_put(motu->unit); 1626c3cef48STakashi Sakamoto } 1636c3cef48STakashi Sakamoto 1646c3cef48STakashi Sakamoto static void motu_bus_update(struct fw_unit *unit) 1656c3cef48STakashi Sakamoto { 1668865a31eSTakashi Sakamoto struct snd_motu *motu = dev_get_drvdata(&unit->device); 1678865a31eSTakashi Sakamoto 1688865a31eSTakashi Sakamoto /* Postpone a workqueue for deferred registration. */ 1698865a31eSTakashi Sakamoto if (!motu->registered) 1708865a31eSTakashi Sakamoto snd_fw_schedule_registration(unit, &motu->dwork); 1712e76701bSTakashi Sakamoto 1722e76701bSTakashi Sakamoto /* The handler address register becomes initialized. */ 1732e76701bSTakashi Sakamoto snd_motu_transaction_reregister(motu); 1746c3cef48STakashi Sakamoto } 1756c3cef48STakashi Sakamoto 176782fbec7STakashi Sakamoto static const struct snd_motu_spec motu_828mk2 = { 177949613e3STakashi Sakamoto .name = "828mk2", 178949613e3STakashi Sakamoto .protocol = &snd_motu_protocol_v2, 179949613e3STakashi Sakamoto .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 180949613e3STakashi Sakamoto SND_MOTU_SPEC_TX_MICINST_CHUNK | 181949613e3STakashi Sakamoto SND_MOTU_SPEC_TX_RETURN_CHUNK | 18281720c6dSTakashi Sakamoto SND_MOTU_SPEC_RX_SEPARETED_MAIN | 183949613e3STakashi Sakamoto SND_MOTU_SPEC_HAS_OPT_IFACE_A | 1848b460c76STakashi Sakamoto SND_MOTU_SPEC_RX_MIDI_2ND_Q | 1858b460c76STakashi Sakamoto SND_MOTU_SPEC_TX_MIDI_2ND_Q, 186949613e3STakashi Sakamoto 187949613e3STakashi Sakamoto .analog_in_ports = 8, 188949613e3STakashi Sakamoto .analog_out_ports = 8, 189949613e3STakashi Sakamoto }; 190949613e3STakashi Sakamoto 1916c5e1ac0STakashi Sakamoto const struct snd_motu_spec snd_motu_spec_traveler = { 1926c5e1ac0STakashi Sakamoto .name = "Traveler", 1936c5e1ac0STakashi Sakamoto .protocol = &snd_motu_protocol_v2, 1946c5e1ac0STakashi Sakamoto .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 1956c5e1ac0STakashi Sakamoto SND_MOTU_SPEC_SUPPORT_CLOCK_X4 | 1966c5e1ac0STakashi Sakamoto SND_MOTU_SPEC_TX_RETURN_CHUNK | 1976c5e1ac0STakashi Sakamoto SND_MOTU_SPEC_HAS_AESEBU_IFACE | 1986c5e1ac0STakashi Sakamoto SND_MOTU_SPEC_HAS_OPT_IFACE_A | 1996c5e1ac0STakashi Sakamoto SND_MOTU_SPEC_RX_MIDI_2ND_Q | 2006c5e1ac0STakashi Sakamoto SND_MOTU_SPEC_TX_MIDI_2ND_Q, 2016c5e1ac0STakashi Sakamoto 2026c5e1ac0STakashi Sakamoto .analog_in_ports = 8, 2036c5e1ac0STakashi Sakamoto .analog_out_ports = 8, 2046c5e1ac0STakashi Sakamoto }; 2056c5e1ac0STakashi Sakamoto 206782fbec7STakashi Sakamoto static const struct snd_motu_spec motu_828mk3 = { 2075992e300STakashi Sakamoto .name = "828mk3", 2085992e300STakashi Sakamoto .protocol = &snd_motu_protocol_v3, 2095992e300STakashi Sakamoto .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 2105992e300STakashi Sakamoto SND_MOTU_SPEC_SUPPORT_CLOCK_X4 | 2115992e300STakashi Sakamoto SND_MOTU_SPEC_TX_MICINST_CHUNK | 2125992e300STakashi Sakamoto SND_MOTU_SPEC_TX_RETURN_CHUNK | 2135992e300STakashi Sakamoto SND_MOTU_SPEC_TX_REVERB_CHUNK | 21481720c6dSTakashi Sakamoto SND_MOTU_SPEC_RX_SEPARETED_MAIN | 2155992e300STakashi Sakamoto SND_MOTU_SPEC_HAS_OPT_IFACE_A | 2165992e300STakashi Sakamoto SND_MOTU_SPEC_HAS_OPT_IFACE_B | 2178b460c76STakashi Sakamoto SND_MOTU_SPEC_RX_MIDI_3RD_Q | 2188b460c76STakashi Sakamoto SND_MOTU_SPEC_TX_MIDI_3RD_Q, 2195992e300STakashi Sakamoto 2205992e300STakashi Sakamoto .analog_in_ports = 8, 2215992e300STakashi Sakamoto .analog_out_ports = 8, 2225992e300STakashi Sakamoto }; 2235992e300STakashi Sakamoto 224782fbec7STakashi Sakamoto static const struct snd_motu_spec motu_audio_express = { 2253a93d082STakashi Sakamoto .name = "AudioExpress", 2263a93d082STakashi Sakamoto .protocol = &snd_motu_protocol_v3, 2273a93d082STakashi Sakamoto .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 2283a93d082STakashi Sakamoto SND_MOTU_SPEC_TX_MICINST_CHUNK | 2293a93d082STakashi Sakamoto SND_MOTU_SPEC_TX_RETURN_CHUNK | 23081720c6dSTakashi Sakamoto SND_MOTU_SPEC_RX_SEPARETED_MAIN | 2313a93d082STakashi Sakamoto SND_MOTU_SPEC_RX_MIDI_2ND_Q | 2323a93d082STakashi Sakamoto SND_MOTU_SPEC_TX_MIDI_3RD_Q, 2333a93d082STakashi Sakamoto .analog_in_ports = 2, 2343a93d082STakashi Sakamoto .analog_out_ports = 4, 2353a93d082STakashi Sakamoto }; 2363a93d082STakashi Sakamoto 2375e03c33eSTakashi Sakamoto #define SND_MOTU_DEV_ENTRY(model, data) \ 2386c3cef48STakashi Sakamoto { \ 2396c3cef48STakashi Sakamoto .match_flags = IEEE1394_MATCH_VENDOR_ID | \ 2406c3cef48STakashi Sakamoto IEEE1394_MATCH_MODEL_ID | \ 2416c3cef48STakashi Sakamoto IEEE1394_MATCH_SPECIFIER_ID, \ 2426c3cef48STakashi Sakamoto .vendor_id = OUI_MOTU, \ 2436c3cef48STakashi Sakamoto .model_id = model, \ 2446c3cef48STakashi Sakamoto .specifier_id = OUI_MOTU, \ 2455e03c33eSTakashi Sakamoto .driver_data = (kernel_ulong_t)data, \ 2466c3cef48STakashi Sakamoto } 2476c3cef48STakashi Sakamoto 2486c3cef48STakashi Sakamoto static const struct ieee1394_device_id motu_id_table[] = { 249949613e3STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x101800, &motu_828mk2), 2506c5e1ac0STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x107800, &snd_motu_spec_traveler), 2515992e300STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x106800, &motu_828mk3), /* FireWire only. */ 2525992e300STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x100800, &motu_828mk3), /* Hybrid. */ 2533a93d082STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x104800, &motu_audio_express), 2546c3cef48STakashi Sakamoto { } 2556c3cef48STakashi Sakamoto }; 2566c3cef48STakashi Sakamoto MODULE_DEVICE_TABLE(ieee1394, motu_id_table); 2576c3cef48STakashi Sakamoto 2586c3cef48STakashi Sakamoto static struct fw_driver motu_driver = { 2596c3cef48STakashi Sakamoto .driver = { 2606c3cef48STakashi Sakamoto .owner = THIS_MODULE, 2616c3cef48STakashi Sakamoto .name = KBUILD_MODNAME, 2626c3cef48STakashi Sakamoto .bus = &fw_bus_type, 2636c3cef48STakashi Sakamoto }, 2646c3cef48STakashi Sakamoto .probe = motu_probe, 2656c3cef48STakashi Sakamoto .update = motu_bus_update, 2666c3cef48STakashi Sakamoto .remove = motu_remove, 2676c3cef48STakashi Sakamoto .id_table = motu_id_table, 2686c3cef48STakashi Sakamoto }; 2696c3cef48STakashi Sakamoto 2706c3cef48STakashi Sakamoto static int __init alsa_motu_init(void) 2716c3cef48STakashi Sakamoto { 2726c3cef48STakashi Sakamoto return driver_register(&motu_driver.driver); 2736c3cef48STakashi Sakamoto } 2746c3cef48STakashi Sakamoto 2756c3cef48STakashi Sakamoto static void __exit alsa_motu_exit(void) 2766c3cef48STakashi Sakamoto { 2776c3cef48STakashi Sakamoto driver_unregister(&motu_driver.driver); 2786c3cef48STakashi Sakamoto } 2796c3cef48STakashi Sakamoto 2806c3cef48STakashi Sakamoto module_init(alsa_motu_init); 2816c3cef48STakashi Sakamoto module_exit(alsa_motu_exit); 282