blob: f2080d720aa9ab3eddd5c29228f52797007ae9bb [file] [log] [blame]
Thomas Gleixnerda607e12019-05-29 16:57:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +09002/*
3 * motu.c - a part of driver for MOTU FireWire series
4 *
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +09006 */
7
8#include "motu.h"
9
10#define OUI_MOTU 0x0001f2
11
12MODULE_DESCRIPTION("MOTU FireWire driver");
13MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
14MODULE_LICENSE("GPL v2");
15
Takashi Sakamoto59f64822017-03-22 21:30:14 +090016const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = {
17 /* mode 0 */
18 [0] = 44100,
19 [1] = 48000,
20 /* mode 1 */
21 [2] = 88200,
22 [3] = 96000,
23 /* mode 2 */
24 [4] = 176400,
25 [5] = 192000,
26};
27
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +090028static void name_card(struct snd_motu *motu)
29{
30 struct fw_device *fw_dev = fw_parent_device(motu->unit);
31 struct fw_csr_iterator it;
32 int key, val;
33 u32 version = 0;
34
35 fw_csr_iterator_init(&it, motu->unit->directory);
36 while (fw_csr_iterator_next(&it, &key, &val)) {
37 switch (key) {
Takashi Sakamoto2d012c62019-03-17 15:49:29 +090038 case CSR_MODEL:
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +090039 version = val;
40 break;
41 }
42 }
43
44 strcpy(motu->card->driver, "FW-MOTU");
Takashi Sakamoto5e03c332017-03-22 21:30:13 +090045 strcpy(motu->card->shortname, motu->spec->name);
46 strcpy(motu->card->mixername, motu->spec->name);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +090047 snprintf(motu->card->longname, sizeof(motu->card->longname),
Takashi Sakamoto2d012c62019-03-17 15:49:29 +090048 "MOTU %s (version:%06x), GUID %08x%08x at %s, S%d",
Takashi Sakamoto5e03c332017-03-22 21:30:13 +090049 motu->spec->name, version,
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +090050 fw_dev->config_rom[3], fw_dev->config_rom[4],
51 dev_name(&motu->unit->device), 100 << fw_dev->max_speed);
52}
53
Takashi Sakamoto8865a312017-03-22 21:30:12 +090054static void motu_card_free(struct snd_card *card)
55{
Takashi Sakamoto3babca42018-10-10 15:35:02 +090056 struct snd_motu *motu = card->private_data;
57
58 snd_motu_transaction_unregister(motu);
59 snd_motu_stream_destroy_duplex(motu);
Takashi Sakamoto8865a312017-03-22 21:30:12 +090060}
61
62static void do_registration(struct work_struct *work)
63{
64 struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work);
65 int err;
66
67 if (motu->registered)
68 return;
69
70 err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
71 &motu->card);
72 if (err < 0)
73 return;
Takashi Sakamoto3babca42018-10-10 15:35:02 +090074 motu->card->private_free = motu_card_free;
75 motu->card->private_data = motu;
Takashi Sakamoto8865a312017-03-22 21:30:12 +090076
77 name_card(motu);
78
Takashi Sakamoto2e767012017-03-22 21:30:19 +090079 err = snd_motu_transaction_register(motu);
80 if (err < 0)
81 goto error;
82
Takashi Sakamoto9b2bb4f2017-03-22 21:30:20 +090083 err = snd_motu_stream_init_duplex(motu);
84 if (err < 0)
85 goto error;
86
Takashi Sakamoto4638ec6e2017-03-22 21:30:21 +090087 snd_motu_proc_init(motu);
88
Takashi Sakamotodd49b2d2017-03-22 21:30:22 +090089 err = snd_motu_create_pcm_devices(motu);
90 if (err < 0)
91 goto error;
92
Takashi Sakamoto8b460c72017-08-20 21:25:03 +090093 if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
94 (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) ||
95 (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
96 (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) {
Takashi Sakamoto9e796e72017-03-22 21:30:23 +090097 err = snd_motu_create_midi_devices(motu);
98 if (err < 0)
99 goto error;
100 }
101
Takashi Sakamoto71c37972017-03-22 21:30:24 +0900102 err = snd_motu_create_hwdep_device(motu);
103 if (err < 0)
104 goto error;
105
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900106 err = snd_card_register(motu->card);
107 if (err < 0)
108 goto error;
109
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900110 motu->registered = true;
111
112 return;
113error:
114 snd_card_free(motu->card);
115 dev_info(&motu->unit->device,
116 "Sound card registration failed: %d\n", err);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900117}
118
119static int motu_probe(struct fw_unit *unit,
120 const struct ieee1394_device_id *entry)
121{
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900122 struct snd_motu *motu;
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900123
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900124 /* Allocate this independently of sound card instance. */
Takashi Sakamoto366a20d2018-10-03 08:21:50 +0900125 motu = devm_kzalloc(&unit->device, sizeof(struct snd_motu), GFP_KERNEL);
126 if (!motu)
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900127 return -ENOMEM;
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900128 motu->unit = fw_unit_get(unit);
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900129 dev_set_drvdata(&unit->device, motu);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900130
Takashi Sakamoto366a20d2018-10-03 08:21:50 +0900131 motu->spec = (const struct snd_motu_spec *)entry->driver_data;
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900132 mutex_init(&motu->mutex);
Takashi Sakamoto9e796e72017-03-22 21:30:23 +0900133 spin_lock_init(&motu->lock);
Takashi Sakamoto71c37972017-03-22 21:30:24 +0900134 init_waitqueue_head(&motu->hwdep_wait);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900135
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900136 /* Allocate and register this sound card later. */
137 INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
138 snd_fw_schedule_registration(unit, &motu->dwork);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900139
140 return 0;
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900141}
142
143static void motu_remove(struct fw_unit *unit)
144{
145 struct snd_motu *motu = dev_get_drvdata(&unit->device);
146
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900147 /*
148 * Confirm to stop the work for registration before the sound card is
149 * going to be released. The work is not scheduled again because bus
150 * reset handler is not called anymore.
151 */
152 cancel_delayed_work_sync(&motu->dwork);
153
154 if (motu->registered) {
Takashi Sakamoto61ccc6f2018-10-10 15:34:59 +0900155 // Block till all of ALSA character devices are released.
156 snd_card_free(motu->card);
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900157 }
Takashi Sakamoto5b14ec22018-10-10 15:35:00 +0900158
159 mutex_destroy(&motu->mutex);
160 fw_unit_put(motu->unit);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900161}
162
163static void motu_bus_update(struct fw_unit *unit)
164{
Takashi Sakamoto8865a312017-03-22 21:30:12 +0900165 struct snd_motu *motu = dev_get_drvdata(&unit->device);
166
167 /* Postpone a workqueue for deferred registration. */
168 if (!motu->registered)
169 snd_fw_schedule_registration(unit, &motu->dwork);
Takashi Sakamoto2e767012017-03-22 21:30:19 +0900170
171 /* The handler address register becomes initialized. */
172 snd_motu_transaction_reregister(motu);
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900173}
174
Takashi Sakamotobd107372019-10-30 17:06:43 +0900175const struct snd_motu_spec snd_motu_spec_828mk2 = {
Takashi Sakamoto949613e2017-03-22 21:30:26 +0900176 .name = "828mk2",
177 .protocol = &snd_motu_protocol_v2,
178 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
179 SND_MOTU_SPEC_TX_MICINST_CHUNK |
180 SND_MOTU_SPEC_TX_RETURN_CHUNK |
Takashi Sakamoto2644df62019-10-30 17:06:39 +0900181 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
Takashi Sakamoto949613e2017-03-22 21:30:26 +0900182 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
Takashi Sakamoto8b460c72017-08-20 21:25:03 +0900183 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
184 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
Takashi Sakamoto949613e2017-03-22 21:30:26 +0900185
186 .analog_in_ports = 8,
187 .analog_out_ports = 8,
188};
189
Takashi Sakamotobd107372019-10-30 17:06:43 +0900190static const struct snd_motu_spec motu_traveler = {
Takashi Sakamoto6c5e1ac2018-06-18 21:07:55 +0900191 .name = "Traveler",
192 .protocol = &snd_motu_protocol_v2,
193 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
194 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
195 SND_MOTU_SPEC_TX_RETURN_CHUNK |
196 SND_MOTU_SPEC_HAS_AESEBU_IFACE |
197 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
198 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
199 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
200
201 .analog_in_ports = 8,
202 .analog_out_ports = 8,
203};
204
Takashi Sakamoto271e6572019-10-30 17:06:44 +0900205static const struct snd_motu_spec motu_ultralite = {
206 .name = "UltraLite",
207 .protocol = &snd_motu_protocol_v2,
208 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
209 SND_MOTU_SPEC_TX_MICINST_CHUNK | // padding.
210 SND_MOTU_SPEC_TX_RETURN_CHUNK |
211 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
212 SND_MOTU_SPEC_TX_MIDI_2ND_Q |
213 SND_MOTU_SPEC_RX_SEPARATED_MAIN,
214 .analog_in_ports = 8,
215 .analog_out_ports = 8,
216};
217
Takashi Sakamotobd107372019-10-30 17:06:43 +0900218static const struct snd_motu_spec motu_8pre = {
Takashi Sakamoto35033d82019-03-17 16:50:24 +0900219 .name = "8pre",
220 .protocol = &snd_motu_protocol_v2,
221 // In tx, use coax chunks for mix-return 1/2. In rx, use coax chunks for
222 // dummy 1/2.
223 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
224 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
225 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
226 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
227 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
228 .analog_in_ports = 8,
229 .analog_out_ports = 2,
230};
231
Takashi Sakamoto782fbec2017-08-22 22:58:15 +0900232static const struct snd_motu_spec motu_828mk3 = {
Takashi Sakamoto5992e302017-03-22 21:30:28 +0900233 .name = "828mk3",
234 .protocol = &snd_motu_protocol_v3,
235 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
236 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
237 SND_MOTU_SPEC_TX_MICINST_CHUNK |
238 SND_MOTU_SPEC_TX_RETURN_CHUNK |
239 SND_MOTU_SPEC_TX_REVERB_CHUNK |
Takashi Sakamoto2644df62019-10-30 17:06:39 +0900240 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
Takashi Sakamoto5992e302017-03-22 21:30:28 +0900241 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
242 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
Takashi Sakamoto8b460c72017-08-20 21:25:03 +0900243 SND_MOTU_SPEC_RX_MIDI_3RD_Q |
244 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
Takashi Sakamoto5992e302017-03-22 21:30:28 +0900245
246 .analog_in_ports = 8,
247 .analog_out_ports = 8,
248};
249
Takashi Sakamoto782fbec2017-08-22 22:58:15 +0900250static const struct snd_motu_spec motu_audio_express = {
Takashi Sakamoto3a93d082017-08-20 21:25:04 +0900251 .name = "AudioExpress",
252 .protocol = &snd_motu_protocol_v3,
253 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
254 SND_MOTU_SPEC_TX_MICINST_CHUNK |
255 SND_MOTU_SPEC_TX_RETURN_CHUNK |
Takashi Sakamoto2644df62019-10-30 17:06:39 +0900256 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
Takashi Sakamoto3a93d082017-08-20 21:25:04 +0900257 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
258 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
259 .analog_in_ports = 2,
260 .analog_out_ports = 4,
261};
262
Takashi Sakamoto6af86bd2019-08-30 10:14:56 +0900263static const struct snd_motu_spec motu_4pre = {
264 .name = "4pre",
265 .protocol = &snd_motu_protocol_v3,
266 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
267 SND_MOTU_SPEC_TX_MICINST_CHUNK |
268 SND_MOTU_SPEC_TX_RETURN_CHUNK |
Takashi Sakamoto2644df62019-10-30 17:06:39 +0900269 SND_MOTU_SPEC_RX_SEPARATED_MAIN,
Takashi Sakamoto6af86bd2019-08-30 10:14:56 +0900270 .analog_in_ports = 2,
271 .analog_out_ports = 2,
272};
273
Takashi Sakamoto5e03c332017-03-22 21:30:13 +0900274#define SND_MOTU_DEV_ENTRY(model, data) \
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900275{ \
276 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
Takashi Sakamoto2d012c62019-03-17 15:49:29 +0900277 IEEE1394_MATCH_SPECIFIER_ID | \
278 IEEE1394_MATCH_VERSION, \
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900279 .vendor_id = OUI_MOTU, \
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900280 .specifier_id = OUI_MOTU, \
Takashi Sakamoto2d012c62019-03-17 15:49:29 +0900281 .version = model, \
Takashi Sakamoto5e03c332017-03-22 21:30:13 +0900282 .driver_data = (kernel_ulong_t)data, \
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900283}
284
285static const struct ieee1394_device_id motu_id_table[] = {
Takashi Sakamotobd107372019-10-30 17:06:43 +0900286 SND_MOTU_DEV_ENTRY(0x000003, &snd_motu_spec_828mk2),
287 SND_MOTU_DEV_ENTRY(0x000009, &motu_traveler),
Takashi Sakamoto271e6572019-10-30 17:06:44 +0900288 SND_MOTU_DEV_ENTRY(0x00000d, &motu_ultralite),
Takashi Sakamotobd107372019-10-30 17:06:43 +0900289 SND_MOTU_DEV_ENTRY(0x00000f, &motu_8pre),
Takashi Sakamoto2d012c62019-03-17 15:49:29 +0900290 SND_MOTU_DEV_ENTRY(0x000015, &motu_828mk3), /* FireWire only. */
291 SND_MOTU_DEV_ENTRY(0x000035, &motu_828mk3), /* Hybrid. */
292 SND_MOTU_DEV_ENTRY(0x000033, &motu_audio_express),
Takashi Sakamoto6af86bd2019-08-30 10:14:56 +0900293 SND_MOTU_DEV_ENTRY(0x000045, &motu_4pre),
Takashi Sakamoto6c3cef42017-03-22 21:30:11 +0900294 { }
295};
296MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
297
298static struct fw_driver motu_driver = {
299 .driver = {
300 .owner = THIS_MODULE,
301 .name = KBUILD_MODNAME,
302 .bus = &fw_bus_type,
303 },
304 .probe = motu_probe,
305 .update = motu_bus_update,
306 .remove = motu_remove,
307 .id_table = motu_id_table,
308};
309
310static int __init alsa_motu_init(void)
311{
312 return driver_register(&motu_driver.driver);
313}
314
315static void __exit alsa_motu_exit(void)
316{
317 driver_unregister(&motu_driver.driver);
318}
319
320module_init(alsa_motu_init);
321module_exit(alsa_motu_exit);