1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 26c3cef48STakashi Sakamoto /* 36c3cef48STakashi Sakamoto * motu.c - a part of driver for MOTU FireWire series 46c3cef48STakashi Sakamoto * 56c3cef48STakashi Sakamoto * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> 66c3cef48STakashi Sakamoto */ 76c3cef48STakashi Sakamoto 86c3cef48STakashi Sakamoto #include "motu.h" 96c3cef48STakashi Sakamoto 106c3cef48STakashi Sakamoto #define OUI_MOTU 0x0001f2 116c3cef48STakashi Sakamoto 126c3cef48STakashi Sakamoto MODULE_DESCRIPTION("MOTU FireWire driver"); 136c3cef48STakashi Sakamoto MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>"); 146c3cef48STakashi Sakamoto MODULE_LICENSE("GPL v2"); 156c3cef48STakashi Sakamoto 1659f6482cSTakashi Sakamoto const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = { 1759f6482cSTakashi Sakamoto /* mode 0 */ 1859f6482cSTakashi Sakamoto [0] = 44100, 1959f6482cSTakashi Sakamoto [1] = 48000, 2059f6482cSTakashi Sakamoto /* mode 1 */ 2159f6482cSTakashi Sakamoto [2] = 88200, 2259f6482cSTakashi Sakamoto [3] = 96000, 2359f6482cSTakashi Sakamoto /* mode 2 */ 2459f6482cSTakashi Sakamoto [4] = 176400, 2559f6482cSTakashi Sakamoto [5] = 192000, 2659f6482cSTakashi Sakamoto }; 2759f6482cSTakashi Sakamoto 286c3cef48STakashi Sakamoto static void name_card(struct snd_motu *motu) 296c3cef48STakashi Sakamoto { 306c3cef48STakashi Sakamoto struct fw_device *fw_dev = fw_parent_device(motu->unit); 316c3cef48STakashi Sakamoto struct fw_csr_iterator it; 326c3cef48STakashi Sakamoto int key, val; 336c3cef48STakashi Sakamoto u32 version = 0; 346c3cef48STakashi Sakamoto 356c3cef48STakashi Sakamoto fw_csr_iterator_init(&it, motu->unit->directory); 366c3cef48STakashi Sakamoto while (fw_csr_iterator_next(&it, &key, &val)) { 376c3cef48STakashi Sakamoto switch (key) { 382d012c65STakashi Sakamoto case CSR_MODEL: 396c3cef48STakashi Sakamoto version = val; 406c3cef48STakashi Sakamoto break; 416c3cef48STakashi Sakamoto } 426c3cef48STakashi Sakamoto } 436c3cef48STakashi Sakamoto 446c3cef48STakashi Sakamoto strcpy(motu->card->driver, "FW-MOTU"); 455e03c33eSTakashi Sakamoto strcpy(motu->card->shortname, motu->spec->name); 465e03c33eSTakashi Sakamoto strcpy(motu->card->mixername, motu->spec->name); 476c3cef48STakashi Sakamoto snprintf(motu->card->longname, sizeof(motu->card->longname), 482d012c65STakashi Sakamoto "MOTU %s (version:%06x), GUID %08x%08x at %s, S%d", 495e03c33eSTakashi Sakamoto motu->spec->name, version, 506c3cef48STakashi Sakamoto fw_dev->config_rom[3], fw_dev->config_rom[4], 516c3cef48STakashi Sakamoto dev_name(&motu->unit->device), 100 << fw_dev->max_speed); 526c3cef48STakashi Sakamoto } 536c3cef48STakashi Sakamoto 548865a31eSTakashi Sakamoto static void motu_card_free(struct snd_card *card) 558865a31eSTakashi Sakamoto { 563babca45STakashi Sakamoto struct snd_motu *motu = card->private_data; 573babca45STakashi Sakamoto 583babca45STakashi Sakamoto snd_motu_transaction_unregister(motu); 593babca45STakashi Sakamoto snd_motu_stream_destroy_duplex(motu); 60a49c6766STakashi Sakamoto 61a49c6766STakashi Sakamoto mutex_destroy(&motu->mutex); 62a49c6766STakashi Sakamoto fw_unit_put(motu->unit); 638865a31eSTakashi Sakamoto } 648865a31eSTakashi Sakamoto 65a49c6766STakashi Sakamoto static int motu_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry) 668865a31eSTakashi Sakamoto { 67a49c6766STakashi Sakamoto struct snd_card *card; 68a49c6766STakashi Sakamoto struct snd_motu *motu; 698865a31eSTakashi Sakamoto int err; 708865a31eSTakashi Sakamoto 71a49c6766STakashi Sakamoto err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, sizeof(*motu), &card); 728865a31eSTakashi Sakamoto if (err < 0) 73a49c6766STakashi Sakamoto return err; 74a49c6766STakashi Sakamoto card->private_free = motu_card_free; 75a49c6766STakashi Sakamoto 76a49c6766STakashi Sakamoto motu = card->private_data; 77a49c6766STakashi Sakamoto motu->unit = fw_unit_get(unit); 78a49c6766STakashi Sakamoto dev_set_drvdata(&unit->device, motu); 79a49c6766STakashi Sakamoto motu->card = card; 80a49c6766STakashi Sakamoto 81a49c6766STakashi Sakamoto motu->spec = (const struct snd_motu_spec *)entry->driver_data; 82a49c6766STakashi Sakamoto mutex_init(&motu->mutex); 83a49c6766STakashi Sakamoto spin_lock_init(&motu->lock); 84a49c6766STakashi Sakamoto init_waitqueue_head(&motu->hwdep_wait); 858865a31eSTakashi Sakamoto 868865a31eSTakashi Sakamoto name_card(motu); 878865a31eSTakashi Sakamoto 882e76701bSTakashi Sakamoto err = snd_motu_transaction_register(motu); 892e76701bSTakashi Sakamoto if (err < 0) 902e76701bSTakashi Sakamoto goto error; 912e76701bSTakashi Sakamoto 929b2bb4f2STakashi Sakamoto err = snd_motu_stream_init_duplex(motu); 939b2bb4f2STakashi Sakamoto if (err < 0) 949b2bb4f2STakashi Sakamoto goto error; 959b2bb4f2STakashi Sakamoto 964638ec6eSTakashi Sakamoto snd_motu_proc_init(motu); 974638ec6eSTakashi Sakamoto 98dd49b2d1STakashi Sakamoto err = snd_motu_create_pcm_devices(motu); 99dd49b2d1STakashi Sakamoto if (err < 0) 100dd49b2d1STakashi Sakamoto goto error; 101dd49b2d1STakashi Sakamoto 1028b460c76STakashi Sakamoto if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) || 1038b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) || 1048b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) || 1058b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) { 1069e796e7dSTakashi Sakamoto err = snd_motu_create_midi_devices(motu); 1079e796e7dSTakashi Sakamoto if (err < 0) 1089e796e7dSTakashi Sakamoto goto error; 1099e796e7dSTakashi Sakamoto } 1109e796e7dSTakashi Sakamoto 11171c37977STakashi Sakamoto err = snd_motu_create_hwdep_device(motu); 11271c37977STakashi Sakamoto if (err < 0) 11371c37977STakashi Sakamoto goto error; 11471c37977STakashi Sakamoto 115bea36afaSTakashi Sakamoto if (motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP) { 116bea36afaSTakashi Sakamoto err = snd_motu_register_dsp_message_parser_new(motu); 117bea36afaSTakashi Sakamoto if (err < 0) 118bea36afaSTakashi Sakamoto goto error; 11990b28f3bSTakashi Sakamoto } else if (motu->spec->flags & SND_MOTU_SPEC_COMMAND_DSP) { 12090b28f3bSTakashi Sakamoto err = snd_motu_command_dsp_message_parser_new(motu); 12190b28f3bSTakashi Sakamoto if (err < 0) 12290b28f3bSTakashi Sakamoto goto error; 123bea36afaSTakashi Sakamoto } 124bea36afaSTakashi Sakamoto 125a49c6766STakashi Sakamoto err = snd_card_register(card); 1268865a31eSTakashi Sakamoto if (err < 0) 1278865a31eSTakashi Sakamoto goto error; 1288865a31eSTakashi Sakamoto 1296c3cef48STakashi Sakamoto return 0; 130a49c6766STakashi Sakamoto error: 131a49c6766STakashi Sakamoto snd_card_free(card); 132a49c6766STakashi Sakamoto return err; 1336c3cef48STakashi Sakamoto } 1346c3cef48STakashi Sakamoto 1356c3cef48STakashi Sakamoto static void motu_remove(struct fw_unit *unit) 1366c3cef48STakashi Sakamoto { 1376c3cef48STakashi Sakamoto struct snd_motu *motu = dev_get_drvdata(&unit->device); 1386c3cef48STakashi Sakamoto 13961ccc6f6STakashi Sakamoto // Block till all of ALSA character devices are released. 14061ccc6f6STakashi Sakamoto snd_card_free(motu->card); 1418865a31eSTakashi Sakamoto } 1425b14ec25STakashi Sakamoto 1436c3cef48STakashi Sakamoto static void motu_bus_update(struct fw_unit *unit) 1446c3cef48STakashi Sakamoto { 1458865a31eSTakashi Sakamoto struct snd_motu *motu = dev_get_drvdata(&unit->device); 1468865a31eSTakashi Sakamoto 1472e76701bSTakashi Sakamoto /* The handler address register becomes initialized. */ 1482e76701bSTakashi Sakamoto snd_motu_transaction_reregister(motu); 1496c3cef48STakashi Sakamoto } 1506c3cef48STakashi Sakamoto 1515e03c33eSTakashi Sakamoto #define SND_MOTU_DEV_ENTRY(model, data) \ 1526c3cef48STakashi Sakamoto { \ 1536c3cef48STakashi Sakamoto .match_flags = IEEE1394_MATCH_VENDOR_ID | \ 1542d012c65STakashi Sakamoto IEEE1394_MATCH_SPECIFIER_ID | \ 1552d012c65STakashi Sakamoto IEEE1394_MATCH_VERSION, \ 1566c3cef48STakashi Sakamoto .vendor_id = OUI_MOTU, \ 1576c3cef48STakashi Sakamoto .specifier_id = OUI_MOTU, \ 1582d012c65STakashi Sakamoto .version = model, \ 1595e03c33eSTakashi Sakamoto .driver_data = (kernel_ulong_t)data, \ 1606c3cef48STakashi Sakamoto } 1616c3cef48STakashi Sakamoto 1626c3cef48STakashi Sakamoto static const struct ieee1394_device_id motu_id_table[] = { 163d13d6b28STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000001, &snd_motu_spec_828), 164b431f16fSTakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000002, &snd_motu_spec_896), 165bd107372STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000003, &snd_motu_spec_828mk2), 16623c671beSTakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000005, &snd_motu_spec_896hd), 1670a7c7b47STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000009, &snd_motu_spec_traveler), 1680a7c7b47STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x00000d, &snd_motu_spec_ultralite), 1690a7c7b47STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x00000f, &snd_motu_spec_8pre), 1705b24119eSTakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000015, &snd_motu_spec_828mk3_fw), // FireWire only. 171e0b2db35STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000019, &snd_motu_spec_ultralite_mk3), // FireWire only. 172*bf868be7STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x00001b, &snd_motu_spec_traveler_mk3), 17364584f32STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000030, &snd_motu_spec_ultralite_mk3), // Hybrid. 1745b24119eSTakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000035, &snd_motu_spec_828mk3_hybrid), // Hybrid. 175c806a0e2STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000033, &snd_motu_spec_audio_express), 176c806a0e2STakashi Sakamoto SND_MOTU_DEV_ENTRY(0x000045, &snd_motu_spec_4pre), 1776c3cef48STakashi Sakamoto { } 1786c3cef48STakashi Sakamoto }; 1796c3cef48STakashi Sakamoto MODULE_DEVICE_TABLE(ieee1394, motu_id_table); 1806c3cef48STakashi Sakamoto 1816c3cef48STakashi Sakamoto static struct fw_driver motu_driver = { 1826c3cef48STakashi Sakamoto .driver = { 1836c3cef48STakashi Sakamoto .owner = THIS_MODULE, 1846c3cef48STakashi Sakamoto .name = KBUILD_MODNAME, 1856c3cef48STakashi Sakamoto .bus = &fw_bus_type, 1866c3cef48STakashi Sakamoto }, 1876c3cef48STakashi Sakamoto .probe = motu_probe, 1886c3cef48STakashi Sakamoto .update = motu_bus_update, 1896c3cef48STakashi Sakamoto .remove = motu_remove, 1906c3cef48STakashi Sakamoto .id_table = motu_id_table, 1916c3cef48STakashi Sakamoto }; 1926c3cef48STakashi Sakamoto 1936c3cef48STakashi Sakamoto static int __init alsa_motu_init(void) 1946c3cef48STakashi Sakamoto { 1956c3cef48STakashi Sakamoto return driver_register(&motu_driver.driver); 1966c3cef48STakashi Sakamoto } 1976c3cef48STakashi Sakamoto 1986c3cef48STakashi Sakamoto static void __exit alsa_motu_exit(void) 1996c3cef48STakashi Sakamoto { 2006c3cef48STakashi Sakamoto driver_unregister(&motu_driver.driver); 2016c3cef48STakashi Sakamoto } 2026c3cef48STakashi Sakamoto 2036c3cef48STakashi Sakamoto module_init(alsa_motu_init); 2046c3cef48STakashi Sakamoto module_exit(alsa_motu_exit); 205