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 176c3cef48STakashi Sakamoto static void name_card(struct snd_motu *motu) 186c3cef48STakashi Sakamoto { 196c3cef48STakashi Sakamoto struct fw_device *fw_dev = fw_parent_device(motu->unit); 206c3cef48STakashi Sakamoto struct fw_csr_iterator it; 216c3cef48STakashi Sakamoto int key, val; 226c3cef48STakashi Sakamoto u32 version = 0; 236c3cef48STakashi Sakamoto 246c3cef48STakashi Sakamoto fw_csr_iterator_init(&it, motu->unit->directory); 256c3cef48STakashi Sakamoto while (fw_csr_iterator_next(&it, &key, &val)) { 266c3cef48STakashi Sakamoto switch (key) { 276c3cef48STakashi Sakamoto case CSR_VERSION: 286c3cef48STakashi Sakamoto version = val; 296c3cef48STakashi Sakamoto break; 306c3cef48STakashi Sakamoto } 316c3cef48STakashi Sakamoto } 326c3cef48STakashi Sakamoto 336c3cef48STakashi Sakamoto strcpy(motu->card->driver, "FW-MOTU"); 346c3cef48STakashi Sakamoto snprintf(motu->card->longname, sizeof(motu->card->longname), 356c3cef48STakashi Sakamoto "MOTU (version:%d), GUID %08x%08x at %s, S%d", 366c3cef48STakashi Sakamoto version, 376c3cef48STakashi Sakamoto fw_dev->config_rom[3], fw_dev->config_rom[4], 386c3cef48STakashi Sakamoto dev_name(&motu->unit->device), 100 << fw_dev->max_speed); 396c3cef48STakashi Sakamoto } 406c3cef48STakashi Sakamoto 41*8865a31eSTakashi Sakamoto static void motu_free(struct snd_motu *motu) 426c3cef48STakashi Sakamoto { 436c3cef48STakashi Sakamoto fw_unit_put(motu->unit); 446c3cef48STakashi Sakamoto 456c3cef48STakashi Sakamoto mutex_destroy(&motu->mutex); 46*8865a31eSTakashi Sakamoto kfree(motu); 47*8865a31eSTakashi Sakamoto } 48*8865a31eSTakashi Sakamoto 49*8865a31eSTakashi Sakamoto /* 50*8865a31eSTakashi Sakamoto * This module releases the FireWire unit data after all ALSA character devices 51*8865a31eSTakashi Sakamoto * are released by applications. This is for releasing stream data or finishing 52*8865a31eSTakashi Sakamoto * transactions safely. Thus at returning from .remove(), this module still keep 53*8865a31eSTakashi Sakamoto * references for the unit. 54*8865a31eSTakashi Sakamoto */ 55*8865a31eSTakashi Sakamoto static void motu_card_free(struct snd_card *card) 56*8865a31eSTakashi Sakamoto { 57*8865a31eSTakashi Sakamoto motu_free(card->private_data); 58*8865a31eSTakashi Sakamoto } 59*8865a31eSTakashi Sakamoto 60*8865a31eSTakashi Sakamoto static void do_registration(struct work_struct *work) 61*8865a31eSTakashi Sakamoto { 62*8865a31eSTakashi Sakamoto struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work); 63*8865a31eSTakashi Sakamoto int err; 64*8865a31eSTakashi Sakamoto 65*8865a31eSTakashi Sakamoto if (motu->registered) 66*8865a31eSTakashi Sakamoto return; 67*8865a31eSTakashi Sakamoto 68*8865a31eSTakashi Sakamoto err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0, 69*8865a31eSTakashi Sakamoto &motu->card); 70*8865a31eSTakashi Sakamoto if (err < 0) 71*8865a31eSTakashi Sakamoto return; 72*8865a31eSTakashi Sakamoto 73*8865a31eSTakashi Sakamoto name_card(motu); 74*8865a31eSTakashi Sakamoto 75*8865a31eSTakashi Sakamoto err = snd_card_register(motu->card); 76*8865a31eSTakashi Sakamoto if (err < 0) 77*8865a31eSTakashi Sakamoto goto error; 78*8865a31eSTakashi Sakamoto 79*8865a31eSTakashi Sakamoto /* 80*8865a31eSTakashi Sakamoto * After registered, motu instance can be released corresponding to 81*8865a31eSTakashi Sakamoto * releasing the sound card instance. 82*8865a31eSTakashi Sakamoto */ 83*8865a31eSTakashi Sakamoto motu->card->private_free = motu_card_free; 84*8865a31eSTakashi Sakamoto motu->card->private_data = motu; 85*8865a31eSTakashi Sakamoto motu->registered = true; 86*8865a31eSTakashi Sakamoto 87*8865a31eSTakashi Sakamoto return; 88*8865a31eSTakashi Sakamoto error: 89*8865a31eSTakashi Sakamoto snd_card_free(motu->card); 90*8865a31eSTakashi Sakamoto dev_info(&motu->unit->device, 91*8865a31eSTakashi Sakamoto "Sound card registration failed: %d\n", err); 926c3cef48STakashi Sakamoto } 936c3cef48STakashi Sakamoto 946c3cef48STakashi Sakamoto static int motu_probe(struct fw_unit *unit, 956c3cef48STakashi Sakamoto const struct ieee1394_device_id *entry) 966c3cef48STakashi Sakamoto { 976c3cef48STakashi Sakamoto struct snd_motu *motu; 986c3cef48STakashi Sakamoto 99*8865a31eSTakashi Sakamoto /* Allocate this independently of sound card instance. */ 100*8865a31eSTakashi Sakamoto motu = kzalloc(sizeof(struct snd_motu), GFP_KERNEL); 101*8865a31eSTakashi Sakamoto if (motu == NULL) 102*8865a31eSTakashi Sakamoto return -ENOMEM; 1036c3cef48STakashi Sakamoto 1046c3cef48STakashi Sakamoto motu->unit = fw_unit_get(unit); 105*8865a31eSTakashi Sakamoto dev_set_drvdata(&unit->device, motu); 1066c3cef48STakashi Sakamoto 1076c3cef48STakashi Sakamoto mutex_init(&motu->mutex); 1086c3cef48STakashi Sakamoto 109*8865a31eSTakashi Sakamoto /* Allocate and register this sound card later. */ 110*8865a31eSTakashi Sakamoto INIT_DEFERRABLE_WORK(&motu->dwork, do_registration); 111*8865a31eSTakashi Sakamoto snd_fw_schedule_registration(unit, &motu->dwork); 1126c3cef48STakashi Sakamoto 1136c3cef48STakashi Sakamoto return 0; 1146c3cef48STakashi Sakamoto } 1156c3cef48STakashi Sakamoto 1166c3cef48STakashi Sakamoto static void motu_remove(struct fw_unit *unit) 1176c3cef48STakashi Sakamoto { 1186c3cef48STakashi Sakamoto struct snd_motu *motu = dev_get_drvdata(&unit->device); 1196c3cef48STakashi Sakamoto 120*8865a31eSTakashi Sakamoto /* 121*8865a31eSTakashi Sakamoto * Confirm to stop the work for registration before the sound card is 122*8865a31eSTakashi Sakamoto * going to be released. The work is not scheduled again because bus 123*8865a31eSTakashi Sakamoto * reset handler is not called anymore. 124*8865a31eSTakashi Sakamoto */ 125*8865a31eSTakashi Sakamoto cancel_delayed_work_sync(&motu->dwork); 126*8865a31eSTakashi Sakamoto 127*8865a31eSTakashi Sakamoto if (motu->registered) { 1286c3cef48STakashi Sakamoto /* No need to wait for releasing card object in this context. */ 1296c3cef48STakashi Sakamoto snd_card_free_when_closed(motu->card); 130*8865a31eSTakashi Sakamoto } else { 131*8865a31eSTakashi Sakamoto /* Don't forget this case. */ 132*8865a31eSTakashi Sakamoto motu_free(motu); 133*8865a31eSTakashi Sakamoto } 1346c3cef48STakashi Sakamoto } 1356c3cef48STakashi Sakamoto 1366c3cef48STakashi Sakamoto static void motu_bus_update(struct fw_unit *unit) 1376c3cef48STakashi Sakamoto { 138*8865a31eSTakashi Sakamoto struct snd_motu *motu = dev_get_drvdata(&unit->device); 139*8865a31eSTakashi Sakamoto 140*8865a31eSTakashi Sakamoto /* Postpone a workqueue for deferred registration. */ 141*8865a31eSTakashi Sakamoto if (!motu->registered) 142*8865a31eSTakashi Sakamoto snd_fw_schedule_registration(unit, &motu->dwork); 1436c3cef48STakashi Sakamoto } 1446c3cef48STakashi Sakamoto 1456c3cef48STakashi Sakamoto #define SND_MOTU_DEV_ENTRY(model) \ 1466c3cef48STakashi Sakamoto { \ 1476c3cef48STakashi Sakamoto .match_flags = IEEE1394_MATCH_VENDOR_ID | \ 1486c3cef48STakashi Sakamoto IEEE1394_MATCH_MODEL_ID | \ 1496c3cef48STakashi Sakamoto IEEE1394_MATCH_SPECIFIER_ID, \ 1506c3cef48STakashi Sakamoto .vendor_id = OUI_MOTU, \ 1516c3cef48STakashi Sakamoto .model_id = model, \ 1526c3cef48STakashi Sakamoto .specifier_id = OUI_MOTU, \ 1536c3cef48STakashi Sakamoto } 1546c3cef48STakashi Sakamoto 1556c3cef48STakashi Sakamoto static const struct ieee1394_device_id motu_id_table[] = { 1566c3cef48STakashi Sakamoto { } 1576c3cef48STakashi Sakamoto }; 1586c3cef48STakashi Sakamoto MODULE_DEVICE_TABLE(ieee1394, motu_id_table); 1596c3cef48STakashi Sakamoto 1606c3cef48STakashi Sakamoto static struct fw_driver motu_driver = { 1616c3cef48STakashi Sakamoto .driver = { 1626c3cef48STakashi Sakamoto .owner = THIS_MODULE, 1636c3cef48STakashi Sakamoto .name = KBUILD_MODNAME, 1646c3cef48STakashi Sakamoto .bus = &fw_bus_type, 1656c3cef48STakashi Sakamoto }, 1666c3cef48STakashi Sakamoto .probe = motu_probe, 1676c3cef48STakashi Sakamoto .update = motu_bus_update, 1686c3cef48STakashi Sakamoto .remove = motu_remove, 1696c3cef48STakashi Sakamoto .id_table = motu_id_table, 1706c3cef48STakashi Sakamoto }; 1716c3cef48STakashi Sakamoto 1726c3cef48STakashi Sakamoto static int __init alsa_motu_init(void) 1736c3cef48STakashi Sakamoto { 1746c3cef48STakashi Sakamoto return driver_register(&motu_driver.driver); 1756c3cef48STakashi Sakamoto } 1766c3cef48STakashi Sakamoto 1776c3cef48STakashi Sakamoto static void __exit alsa_motu_exit(void) 1786c3cef48STakashi Sakamoto { 1796c3cef48STakashi Sakamoto driver_unregister(&motu_driver.driver); 1806c3cef48STakashi Sakamoto } 1816c3cef48STakashi Sakamoto 1826c3cef48STakashi Sakamoto module_init(alsa_motu_init); 1836c3cef48STakashi Sakamoto module_exit(alsa_motu_exit); 184