ALSA: firewire-motu: code refactoring to handle model specific switch for protocol v2
[linux-2.6-block.git] / sound / firewire / motu / motu.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
6c3cef48
TS
2/*
3 * motu.c - a part of driver for MOTU FireWire series
4 *
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6c3cef48
TS
6 */
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
59f6482c
TS
16const 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
6c3cef48
TS
28static 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) {
2d012c65 38 case CSR_MODEL:
6c3cef48
TS
39 version = val;
40 break;
41 }
42 }
43
44 strcpy(motu->card->driver, "FW-MOTU");
5e03c33e
TS
45 strcpy(motu->card->shortname, motu->spec->name);
46 strcpy(motu->card->mixername, motu->spec->name);
6c3cef48 47 snprintf(motu->card->longname, sizeof(motu->card->longname),
2d012c65 48 "MOTU %s (version:%06x), GUID %08x%08x at %s, S%d",
5e03c33e 49 motu->spec->name, version,
6c3cef48
TS
50 fw_dev->config_rom[3], fw_dev->config_rom[4],
51 dev_name(&motu->unit->device), 100 << fw_dev->max_speed);
52}
53
3babca45 54static void motu_card_free(struct snd_card *card)
6c3cef48 55{
3babca45 56 struct snd_motu *motu = card->private_data;
2e76701b 57
3babca45 58 snd_motu_transaction_unregister(motu);
9b2bb4f2 59 snd_motu_stream_destroy_duplex(motu);
6c3cef48
TS
60}
61
8865a31e
TS
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;
6c3cef48 66
8865a31e
TS
67 if (motu->registered)
68 return;
6c3cef48 69
8865a31e
TS
70 err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
71 &motu->card);
72 if (err < 0)
73 return;
3babca45
TS
74 motu->card->private_free = motu_card_free;
75 motu->card->private_data = motu;
6c3cef48
TS
76
77 name_card(motu);
78
2e76701b
TS
79 err = snd_motu_transaction_register(motu);
80 if (err < 0)
81 goto error;
82
9b2bb4f2
TS
83 err = snd_motu_stream_init_duplex(motu);
84 if (err < 0)
85 goto error;
86
4638ec6e
TS
87 snd_motu_proc_init(motu);
88
dd49b2d1
TS
89 err = snd_motu_create_pcm_devices(motu);
90 if (err < 0)
91 goto error;
92
8b460c76
TS
93 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)) {
9e796e7d
TS
97 err = snd_motu_create_midi_devices(motu);
98 if (err < 0)
99 goto error;
100 }
101
71c37977
TS
102 err = snd_motu_create_hwdep_device(motu);
103 if (err < 0)
104 goto error;
105
8865a31e 106 err = snd_card_register(motu->card);
6c3cef48
TS
107 if (err < 0)
108 goto error;
109
8865a31e
TS
110 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);
117}
118
119static int motu_probe(struct fw_unit *unit,
120 const struct ieee1394_device_id *entry)
121{
122 struct snd_motu *motu;
123
124 /* Allocate this independently of sound card instance. */
366a20d7
TS
125 motu = devm_kzalloc(&unit->device, sizeof(struct snd_motu), GFP_KERNEL);
126 if (!motu)
8865a31e 127 return -ENOMEM;
8865a31e 128 motu->unit = fw_unit_get(unit);
6c3cef48
TS
129 dev_set_drvdata(&unit->device, motu);
130
366a20d7 131 motu->spec = (const struct snd_motu_spec *)entry->driver_data;
8865a31e 132 mutex_init(&motu->mutex);
9e796e7d 133 spin_lock_init(&motu->lock);
71c37977 134 init_waitqueue_head(&motu->hwdep_wait);
8865a31e
TS
135
136 /* Allocate and register this sound card later. */
137 INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
138 snd_fw_schedule_registration(unit, &motu->dwork);
139
6c3cef48 140 return 0;
6c3cef48
TS
141}
142
143static void motu_remove(struct fw_unit *unit)
144{
145 struct snd_motu *motu = dev_get_drvdata(&unit->device);
146
8865a31e
TS
147 /*
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) {
61ccc6f6
TS
155 // Block till all of ALSA character devices are released.
156 snd_card_free(motu->card);
8865a31e 157 }
5b14ec25
TS
158
159 mutex_destroy(&motu->mutex);
160 fw_unit_put(motu->unit);
6c3cef48
TS
161}
162
163static void motu_bus_update(struct fw_unit *unit)
164{
8865a31e
TS
165 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);
2e76701b
TS
170
171 /* The handler address register becomes initialized. */
172 snd_motu_transaction_reregister(motu);
6c3cef48
TS
173}
174
bd107372 175const struct snd_motu_spec snd_motu_spec_828mk2 = {
949613e3
TS
176 .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 |
2644df63 181 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
949613e3 182 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
8b460c76
TS
183 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
184 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
949613e3
TS
185
186 .analog_in_ports = 8,
187 .analog_out_ports = 8,
188};
189
bd107372 190static const struct snd_motu_spec motu_traveler = {
6c5e1ac0
TS
191 .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
bd107372 205static const struct snd_motu_spec motu_8pre = {
35033d8c
TS
206 .name = "8pre",
207 .protocol = &snd_motu_protocol_v2,
208 // In tx, use coax chunks for mix-return 1/2. In rx, use coax chunks for
209 // dummy 1/2.
210 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
211 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
212 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
213 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
214 SND_MOTU_SPEC_TX_MIDI_2ND_Q,
215 .analog_in_ports = 8,
216 .analog_out_ports = 2,
217};
218
782fbec7 219static const struct snd_motu_spec motu_828mk3 = {
5992e300
TS
220 .name = "828mk3",
221 .protocol = &snd_motu_protocol_v3,
222 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
223 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
224 SND_MOTU_SPEC_TX_MICINST_CHUNK |
225 SND_MOTU_SPEC_TX_RETURN_CHUNK |
226 SND_MOTU_SPEC_TX_REVERB_CHUNK |
2644df63 227 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
5992e300
TS
228 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
229 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
8b460c76
TS
230 SND_MOTU_SPEC_RX_MIDI_3RD_Q |
231 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
5992e300
TS
232
233 .analog_in_ports = 8,
234 .analog_out_ports = 8,
235};
236
782fbec7 237static const struct snd_motu_spec motu_audio_express = {
3a93d082
TS
238 .name = "AudioExpress",
239 .protocol = &snd_motu_protocol_v3,
240 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
241 SND_MOTU_SPEC_TX_MICINST_CHUNK |
242 SND_MOTU_SPEC_TX_RETURN_CHUNK |
2644df63 243 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
3a93d082
TS
244 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
245 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
246 .analog_in_ports = 2,
247 .analog_out_ports = 4,
248};
249
6af86bdb
TS
250static const struct snd_motu_spec motu_4pre = {
251 .name = "4pre",
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 |
2644df63 256 SND_MOTU_SPEC_RX_SEPARATED_MAIN,
6af86bdb
TS
257 .analog_in_ports = 2,
258 .analog_out_ports = 2,
259};
260
5e03c33e 261#define SND_MOTU_DEV_ENTRY(model, data) \
6c3cef48
TS
262{ \
263 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
2d012c65
TS
264 IEEE1394_MATCH_SPECIFIER_ID | \
265 IEEE1394_MATCH_VERSION, \
6c3cef48 266 .vendor_id = OUI_MOTU, \
6c3cef48 267 .specifier_id = OUI_MOTU, \
2d012c65 268 .version = model, \
5e03c33e 269 .driver_data = (kernel_ulong_t)data, \
6c3cef48
TS
270}
271
272static const struct ieee1394_device_id motu_id_table[] = {
bd107372
TS
273 SND_MOTU_DEV_ENTRY(0x000003, &snd_motu_spec_828mk2),
274 SND_MOTU_DEV_ENTRY(0x000009, &motu_traveler),
275 SND_MOTU_DEV_ENTRY(0x00000f, &motu_8pre),
2d012c65
TS
276 SND_MOTU_DEV_ENTRY(0x000015, &motu_828mk3), /* FireWire only. */
277 SND_MOTU_DEV_ENTRY(0x000035, &motu_828mk3), /* Hybrid. */
278 SND_MOTU_DEV_ENTRY(0x000033, &motu_audio_express),
6af86bdb 279 SND_MOTU_DEV_ENTRY(0x000045, &motu_4pre),
6c3cef48
TS
280 { }
281};
282MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
283
284static struct fw_driver motu_driver = {
285 .driver = {
286 .owner = THIS_MODULE,
287 .name = KBUILD_MODNAME,
288 .bus = &fw_bus_type,
289 },
290 .probe = motu_probe,
291 .update = motu_bus_update,
292 .remove = motu_remove,
293 .id_table = motu_id_table,
294};
295
296static int __init alsa_motu_init(void)
297{
298 return driver_register(&motu_driver.driver);
299}
300
301static void __exit alsa_motu_exit(void)
302{
303 driver_unregister(&motu_driver.driver);
304}
305
306module_init(alsa_motu_init);
307module_exit(alsa_motu_exit);