xref: /openbmc/linux/sound/firewire/motu/motu.c (revision bd1073728260e1d2f9589c928b6d8ddb14f5646f)
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);
608865a31eSTakashi Sakamoto }
618865a31eSTakashi Sakamoto 
628865a31eSTakashi Sakamoto static void do_registration(struct work_struct *work)
638865a31eSTakashi Sakamoto {
648865a31eSTakashi Sakamoto 	struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work);
658865a31eSTakashi Sakamoto 	int err;
668865a31eSTakashi Sakamoto 
678865a31eSTakashi Sakamoto 	if (motu->registered)
688865a31eSTakashi Sakamoto 		return;
698865a31eSTakashi Sakamoto 
708865a31eSTakashi Sakamoto 	err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
718865a31eSTakashi Sakamoto 			   &motu->card);
728865a31eSTakashi Sakamoto 	if (err < 0)
738865a31eSTakashi Sakamoto 		return;
743babca45STakashi Sakamoto 	motu->card->private_free = motu_card_free;
753babca45STakashi Sakamoto 	motu->card->private_data = motu;
768865a31eSTakashi Sakamoto 
778865a31eSTakashi Sakamoto 	name_card(motu);
788865a31eSTakashi Sakamoto 
792e76701bSTakashi Sakamoto 	err = snd_motu_transaction_register(motu);
802e76701bSTakashi Sakamoto 	if (err < 0)
812e76701bSTakashi Sakamoto 		goto error;
822e76701bSTakashi Sakamoto 
839b2bb4f2STakashi Sakamoto 	err = snd_motu_stream_init_duplex(motu);
849b2bb4f2STakashi Sakamoto 	if (err < 0)
859b2bb4f2STakashi Sakamoto 		goto error;
869b2bb4f2STakashi Sakamoto 
874638ec6eSTakashi Sakamoto 	snd_motu_proc_init(motu);
884638ec6eSTakashi Sakamoto 
89dd49b2d1STakashi Sakamoto 	err = snd_motu_create_pcm_devices(motu);
90dd49b2d1STakashi Sakamoto 	if (err < 0)
91dd49b2d1STakashi Sakamoto 		goto error;
92dd49b2d1STakashi Sakamoto 
938b460c76STakashi Sakamoto 	if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
948b460c76STakashi Sakamoto 	    (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) ||
958b460c76STakashi Sakamoto 	    (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
968b460c76STakashi Sakamoto 	    (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) {
979e796e7dSTakashi Sakamoto 		err = snd_motu_create_midi_devices(motu);
989e796e7dSTakashi Sakamoto 		if (err < 0)
999e796e7dSTakashi Sakamoto 			goto error;
1009e796e7dSTakashi Sakamoto 	}
1019e796e7dSTakashi Sakamoto 
10271c37977STakashi Sakamoto 	err = snd_motu_create_hwdep_device(motu);
10371c37977STakashi Sakamoto 	if (err < 0)
10471c37977STakashi Sakamoto 		goto error;
10571c37977STakashi Sakamoto 
1068865a31eSTakashi Sakamoto 	err = snd_card_register(motu->card);
1078865a31eSTakashi Sakamoto 	if (err < 0)
1088865a31eSTakashi Sakamoto 		goto error;
1098865a31eSTakashi Sakamoto 
1108865a31eSTakashi Sakamoto 	motu->registered = true;
1118865a31eSTakashi Sakamoto 
1128865a31eSTakashi Sakamoto 	return;
1138865a31eSTakashi Sakamoto error:
1148865a31eSTakashi Sakamoto 	snd_card_free(motu->card);
1158865a31eSTakashi Sakamoto 	dev_info(&motu->unit->device,
1168865a31eSTakashi Sakamoto 		 "Sound card registration failed: %d\n", err);
1176c3cef48STakashi Sakamoto }
1186c3cef48STakashi Sakamoto 
1196c3cef48STakashi Sakamoto static int motu_probe(struct fw_unit *unit,
1206c3cef48STakashi Sakamoto 		      const struct ieee1394_device_id *entry)
1216c3cef48STakashi Sakamoto {
1226c3cef48STakashi Sakamoto 	struct snd_motu *motu;
1236c3cef48STakashi Sakamoto 
1248865a31eSTakashi Sakamoto 	/* Allocate this independently of sound card instance. */
125366a20d7STakashi Sakamoto 	motu = devm_kzalloc(&unit->device, sizeof(struct snd_motu), GFP_KERNEL);
126366a20d7STakashi Sakamoto 	if (!motu)
1278865a31eSTakashi Sakamoto 		return -ENOMEM;
1286c3cef48STakashi Sakamoto 	motu->unit = fw_unit_get(unit);
1298865a31eSTakashi Sakamoto 	dev_set_drvdata(&unit->device, motu);
1306c3cef48STakashi Sakamoto 
131366a20d7STakashi Sakamoto 	motu->spec = (const struct snd_motu_spec *)entry->driver_data;
1326c3cef48STakashi Sakamoto 	mutex_init(&motu->mutex);
1339e796e7dSTakashi Sakamoto 	spin_lock_init(&motu->lock);
13471c37977STakashi Sakamoto 	init_waitqueue_head(&motu->hwdep_wait);
1356c3cef48STakashi Sakamoto 
1368865a31eSTakashi Sakamoto 	/* Allocate and register this sound card later. */
1378865a31eSTakashi Sakamoto 	INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
1388865a31eSTakashi Sakamoto 	snd_fw_schedule_registration(unit, &motu->dwork);
1396c3cef48STakashi Sakamoto 
1406c3cef48STakashi Sakamoto 	return 0;
1416c3cef48STakashi Sakamoto }
1426c3cef48STakashi Sakamoto 
1436c3cef48STakashi Sakamoto static void motu_remove(struct fw_unit *unit)
1446c3cef48STakashi Sakamoto {
1456c3cef48STakashi Sakamoto 	struct snd_motu *motu = dev_get_drvdata(&unit->device);
1466c3cef48STakashi Sakamoto 
1478865a31eSTakashi Sakamoto 	/*
1488865a31eSTakashi Sakamoto 	 * Confirm to stop the work for registration before the sound card is
1498865a31eSTakashi Sakamoto 	 * going to be released. The work is not scheduled again because bus
1508865a31eSTakashi Sakamoto 	 * reset handler is not called anymore.
1518865a31eSTakashi Sakamoto 	 */
1528865a31eSTakashi Sakamoto 	cancel_delayed_work_sync(&motu->dwork);
1538865a31eSTakashi Sakamoto 
1548865a31eSTakashi Sakamoto 	if (motu->registered) {
15561ccc6f6STakashi Sakamoto 		// Block till all of ALSA character devices are released.
15661ccc6f6STakashi Sakamoto 		snd_card_free(motu->card);
1578865a31eSTakashi Sakamoto 	}
1585b14ec25STakashi Sakamoto 
1595b14ec25STakashi Sakamoto 	mutex_destroy(&motu->mutex);
1605b14ec25STakashi Sakamoto 	fw_unit_put(motu->unit);
1616c3cef48STakashi Sakamoto }
1626c3cef48STakashi Sakamoto 
1636c3cef48STakashi Sakamoto static void motu_bus_update(struct fw_unit *unit)
1646c3cef48STakashi Sakamoto {
1658865a31eSTakashi Sakamoto 	struct snd_motu *motu = dev_get_drvdata(&unit->device);
1668865a31eSTakashi Sakamoto 
1678865a31eSTakashi Sakamoto 	/* Postpone a workqueue for deferred registration. */
1688865a31eSTakashi Sakamoto 	if (!motu->registered)
1698865a31eSTakashi Sakamoto 		snd_fw_schedule_registration(unit, &motu->dwork);
1702e76701bSTakashi Sakamoto 
1712e76701bSTakashi Sakamoto 	/* The handler address register becomes initialized. */
1722e76701bSTakashi Sakamoto 	snd_motu_transaction_reregister(motu);
1736c3cef48STakashi Sakamoto }
1746c3cef48STakashi Sakamoto 
175*bd107372STakashi Sakamoto const struct snd_motu_spec snd_motu_spec_828mk2 = {
176949613e3STakashi Sakamoto 	.name = "828mk2",
177949613e3STakashi Sakamoto 	.protocol = &snd_motu_protocol_v2,
178949613e3STakashi Sakamoto 	.flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
179949613e3STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MICINST_CHUNK |
180949613e3STakashi Sakamoto 		 SND_MOTU_SPEC_TX_RETURN_CHUNK |
1812644df63STakashi Sakamoto 		 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
182949613e3STakashi Sakamoto 		 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
1838b460c76STakashi Sakamoto 		 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
1848b460c76STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
185949613e3STakashi Sakamoto 
186949613e3STakashi Sakamoto 	.analog_in_ports = 8,
187949613e3STakashi Sakamoto 	.analog_out_ports = 8,
188949613e3STakashi Sakamoto };
189949613e3STakashi Sakamoto 
190*bd107372STakashi Sakamoto static const struct snd_motu_spec motu_traveler = {
1916c5e1ac0STakashi Sakamoto 	.name = "Traveler",
1926c5e1ac0STakashi Sakamoto 	.protocol = &snd_motu_protocol_v2,
1936c5e1ac0STakashi Sakamoto 	.flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
1946c5e1ac0STakashi Sakamoto 		 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
1956c5e1ac0STakashi Sakamoto 		 SND_MOTU_SPEC_TX_RETURN_CHUNK |
1966c5e1ac0STakashi Sakamoto 		 SND_MOTU_SPEC_HAS_AESEBU_IFACE |
1976c5e1ac0STakashi Sakamoto 		 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
1986c5e1ac0STakashi Sakamoto 		 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
1996c5e1ac0STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
2006c5e1ac0STakashi Sakamoto 
2016c5e1ac0STakashi Sakamoto 	.analog_in_ports = 8,
2026c5e1ac0STakashi Sakamoto 	.analog_out_ports = 8,
2036c5e1ac0STakashi Sakamoto };
2046c5e1ac0STakashi Sakamoto 
205*bd107372STakashi Sakamoto static const struct snd_motu_spec motu_8pre = {
20635033d8cSTakashi Sakamoto 	.name = "8pre",
20735033d8cSTakashi Sakamoto 	.protocol = &snd_motu_protocol_v2,
20835033d8cSTakashi Sakamoto 	// In tx, use coax chunks for mix-return 1/2. In rx, use coax chunks for
20935033d8cSTakashi Sakamoto 	// dummy 1/2.
21035033d8cSTakashi Sakamoto 	.flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
21135033d8cSTakashi Sakamoto 		 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
21235033d8cSTakashi Sakamoto 		 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
21335033d8cSTakashi Sakamoto 		 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
21435033d8cSTakashi Sakamoto 		 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
21535033d8cSTakashi Sakamoto 	.analog_in_ports = 8,
21635033d8cSTakashi Sakamoto 	.analog_out_ports = 2,
21735033d8cSTakashi Sakamoto };
21835033d8cSTakashi Sakamoto 
219782fbec7STakashi Sakamoto static const struct snd_motu_spec motu_828mk3 = {
2205992e300STakashi Sakamoto 	.name = "828mk3",
2215992e300STakashi Sakamoto 	.protocol = &snd_motu_protocol_v3,
2225992e300STakashi Sakamoto 	.flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
2235992e300STakashi Sakamoto 		 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
2245992e300STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MICINST_CHUNK |
2255992e300STakashi Sakamoto 		 SND_MOTU_SPEC_TX_RETURN_CHUNK |
2265992e300STakashi Sakamoto 		 SND_MOTU_SPEC_TX_REVERB_CHUNK |
2272644df63STakashi Sakamoto 		 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
2285992e300STakashi Sakamoto 		 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
2295992e300STakashi Sakamoto 		 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
2308b460c76STakashi Sakamoto 		 SND_MOTU_SPEC_RX_MIDI_3RD_Q |
2318b460c76STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
2325992e300STakashi Sakamoto 
2335992e300STakashi Sakamoto 	.analog_in_ports = 8,
2345992e300STakashi Sakamoto 	.analog_out_ports = 8,
2355992e300STakashi Sakamoto };
2365992e300STakashi Sakamoto 
237782fbec7STakashi Sakamoto static const struct snd_motu_spec motu_audio_express = {
2383a93d082STakashi Sakamoto 	.name = "AudioExpress",
2393a93d082STakashi Sakamoto 	.protocol = &snd_motu_protocol_v3,
2403a93d082STakashi Sakamoto 	.flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
2413a93d082STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MICINST_CHUNK |
2423a93d082STakashi Sakamoto 		 SND_MOTU_SPEC_TX_RETURN_CHUNK |
2432644df63STakashi Sakamoto 		 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
2443a93d082STakashi Sakamoto 		 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
2453a93d082STakashi Sakamoto 		 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
2463a93d082STakashi Sakamoto 	.analog_in_ports = 2,
2473a93d082STakashi Sakamoto 	.analog_out_ports = 4,
2483a93d082STakashi Sakamoto };
2493a93d082STakashi Sakamoto 
2506af86bdbSTakashi Sakamoto static const struct snd_motu_spec motu_4pre = {
2516af86bdbSTakashi Sakamoto 	.name = "4pre",
2526af86bdbSTakashi Sakamoto 	.protocol = &snd_motu_protocol_v3,
2536af86bdbSTakashi Sakamoto 	.flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
2546af86bdbSTakashi Sakamoto 		 SND_MOTU_SPEC_TX_MICINST_CHUNK |
2556af86bdbSTakashi Sakamoto 		 SND_MOTU_SPEC_TX_RETURN_CHUNK |
2562644df63STakashi Sakamoto 		 SND_MOTU_SPEC_RX_SEPARATED_MAIN,
2576af86bdbSTakashi Sakamoto 	.analog_in_ports = 2,
2586af86bdbSTakashi Sakamoto 	.analog_out_ports = 2,
2596af86bdbSTakashi Sakamoto };
2606af86bdbSTakashi Sakamoto 
2615e03c33eSTakashi Sakamoto #define SND_MOTU_DEV_ENTRY(model, data)			\
2626c3cef48STakashi Sakamoto {							\
2636c3cef48STakashi Sakamoto 	.match_flags	= IEEE1394_MATCH_VENDOR_ID |	\
2642d012c65STakashi Sakamoto 			  IEEE1394_MATCH_SPECIFIER_ID |	\
2652d012c65STakashi Sakamoto 			  IEEE1394_MATCH_VERSION,	\
2666c3cef48STakashi Sakamoto 	.vendor_id	= OUI_MOTU,			\
2676c3cef48STakashi Sakamoto 	.specifier_id	= OUI_MOTU,			\
2682d012c65STakashi Sakamoto 	.version	= model,			\
2695e03c33eSTakashi Sakamoto 	.driver_data	= (kernel_ulong_t)data,		\
2706c3cef48STakashi Sakamoto }
2716c3cef48STakashi Sakamoto 
2726c3cef48STakashi Sakamoto static const struct ieee1394_device_id motu_id_table[] = {
273*bd107372STakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x000003, &snd_motu_spec_828mk2),
274*bd107372STakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x000009, &motu_traveler),
275*bd107372STakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x00000f, &motu_8pre),
2762d012c65STakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x000015, &motu_828mk3),	/* FireWire only. */
2772d012c65STakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x000035, &motu_828mk3),	/* Hybrid. */
2782d012c65STakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x000033, &motu_audio_express),
2796af86bdbSTakashi Sakamoto 	SND_MOTU_DEV_ENTRY(0x000045, &motu_4pre),
2806c3cef48STakashi Sakamoto 	{ }
2816c3cef48STakashi Sakamoto };
2826c3cef48STakashi Sakamoto MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
2836c3cef48STakashi Sakamoto 
2846c3cef48STakashi Sakamoto static struct fw_driver motu_driver = {
2856c3cef48STakashi Sakamoto 	.driver   = {
2866c3cef48STakashi Sakamoto 		.owner	= THIS_MODULE,
2876c3cef48STakashi Sakamoto 		.name	= KBUILD_MODNAME,
2886c3cef48STakashi Sakamoto 		.bus	= &fw_bus_type,
2896c3cef48STakashi Sakamoto 	},
2906c3cef48STakashi Sakamoto 	.probe    = motu_probe,
2916c3cef48STakashi Sakamoto 	.update   = motu_bus_update,
2926c3cef48STakashi Sakamoto 	.remove   = motu_remove,
2936c3cef48STakashi Sakamoto 	.id_table = motu_id_table,
2946c3cef48STakashi Sakamoto };
2956c3cef48STakashi Sakamoto 
2966c3cef48STakashi Sakamoto static int __init alsa_motu_init(void)
2976c3cef48STakashi Sakamoto {
2986c3cef48STakashi Sakamoto 	return driver_register(&motu_driver.driver);
2996c3cef48STakashi Sakamoto }
3006c3cef48STakashi Sakamoto 
3016c3cef48STakashi Sakamoto static void __exit alsa_motu_exit(void)
3026c3cef48STakashi Sakamoto {
3036c3cef48STakashi Sakamoto 	driver_unregister(&motu_driver.driver);
3046c3cef48STakashi Sakamoto }
3056c3cef48STakashi Sakamoto 
3066c3cef48STakashi Sakamoto module_init(alsa_motu_init);
3076c3cef48STakashi Sakamoto module_exit(alsa_motu_exit);
308