]>
Commit | Line | Data |
---|---|---|
6c3cef48 TS |
1 | /* |
2 | * motu.c - a part of driver for MOTU FireWire series | |
3 | * | |
4 | * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> | |
5 | * | |
6 | * Licensed under the terms of the GNU General Public License, version 2. | |
7 | */ | |
8 | ||
9 | #include "motu.h" | |
10 | ||
11 | #define OUI_MOTU 0x0001f2 | |
12 | ||
13 | MODULE_DESCRIPTION("MOTU FireWire driver"); | |
14 | MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>"); | |
15 | MODULE_LICENSE("GPL v2"); | |
16 | ||
59f6482c TS |
17 | const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = { |
18 | /* mode 0 */ | |
19 | [0] = 44100, | |
20 | [1] = 48000, | |
21 | /* mode 1 */ | |
22 | [2] = 88200, | |
23 | [3] = 96000, | |
24 | /* mode 2 */ | |
25 | [4] = 176400, | |
26 | [5] = 192000, | |
27 | }; | |
28 | ||
6c3cef48 TS |
29 | static void name_card(struct snd_motu *motu) |
30 | { | |
31 | struct fw_device *fw_dev = fw_parent_device(motu->unit); | |
32 | struct fw_csr_iterator it; | |
33 | int key, val; | |
34 | u32 version = 0; | |
35 | ||
36 | fw_csr_iterator_init(&it, motu->unit->directory); | |
37 | while (fw_csr_iterator_next(&it, &key, &val)) { | |
38 | switch (key) { | |
39 | case CSR_VERSION: | |
40 | version = val; | |
41 | break; | |
42 | } | |
43 | } | |
44 | ||
45 | strcpy(motu->card->driver, "FW-MOTU"); | |
5e03c33e TS |
46 | strcpy(motu->card->shortname, motu->spec->name); |
47 | strcpy(motu->card->mixername, motu->spec->name); | |
6c3cef48 | 48 | snprintf(motu->card->longname, sizeof(motu->card->longname), |
5e03c33e TS |
49 | "MOTU %s (version:%d), GUID %08x%08x at %s, S%d", |
50 | motu->spec->name, version, | |
6c3cef48 TS |
51 | fw_dev->config_rom[3], fw_dev->config_rom[4], |
52 | dev_name(&motu->unit->device), 100 << fw_dev->max_speed); | |
53 | } | |
54 | ||
8865a31e | 55 | static void motu_free(struct snd_motu *motu) |
6c3cef48 | 56 | { |
2e76701b TS |
57 | snd_motu_transaction_unregister(motu); |
58 | ||
9b2bb4f2 | 59 | snd_motu_stream_destroy_duplex(motu); |
6c3cef48 TS |
60 | fw_unit_put(motu->unit); |
61 | ||
62 | mutex_destroy(&motu->mutex); | |
8865a31e | 63 | kfree(motu); |
6c3cef48 TS |
64 | } |
65 | ||
8865a31e TS |
66 | /* |
67 | * This module releases the FireWire unit data after all ALSA character devices | |
68 | * are released by applications. This is for releasing stream data or finishing | |
69 | * transactions safely. Thus at returning from .remove(), this module still keep | |
70 | * references for the unit. | |
71 | */ | |
72 | static void motu_card_free(struct snd_card *card) | |
6c3cef48 | 73 | { |
8865a31e TS |
74 | motu_free(card->private_data); |
75 | } | |
6c3cef48 | 76 | |
8865a31e TS |
77 | static void do_registration(struct work_struct *work) |
78 | { | |
79 | struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work); | |
80 | int err; | |
6c3cef48 | 81 | |
8865a31e TS |
82 | if (motu->registered) |
83 | return; | |
6c3cef48 | 84 | |
8865a31e TS |
85 | err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0, |
86 | &motu->card); | |
87 | if (err < 0) | |
88 | return; | |
6c3cef48 TS |
89 | |
90 | name_card(motu); | |
91 | ||
2e76701b TS |
92 | err = snd_motu_transaction_register(motu); |
93 | if (err < 0) | |
94 | goto error; | |
95 | ||
9b2bb4f2 TS |
96 | err = snd_motu_stream_init_duplex(motu); |
97 | if (err < 0) | |
98 | goto error; | |
99 | ||
4638ec6e TS |
100 | snd_motu_proc_init(motu); |
101 | ||
dd49b2d1 TS |
102 | err = snd_motu_create_pcm_devices(motu); |
103 | if (err < 0) | |
104 | goto error; | |
105 | ||
8b460c76 TS |
106 | if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) || |
107 | (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) || | |
108 | (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) || | |
109 | (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) { | |
9e796e7d TS |
110 | err = snd_motu_create_midi_devices(motu); |
111 | if (err < 0) | |
112 | goto error; | |
113 | } | |
114 | ||
71c37977 TS |
115 | err = snd_motu_create_hwdep_device(motu); |
116 | if (err < 0) | |
117 | goto error; | |
118 | ||
8865a31e | 119 | err = snd_card_register(motu->card); |
6c3cef48 TS |
120 | if (err < 0) |
121 | goto error; | |
122 | ||
8865a31e TS |
123 | /* |
124 | * After registered, motu instance can be released corresponding to | |
125 | * releasing the sound card instance. | |
126 | */ | |
127 | motu->card->private_free = motu_card_free; | |
128 | motu->card->private_data = motu; | |
129 | motu->registered = true; | |
130 | ||
131 | return; | |
132 | error: | |
2e76701b | 133 | snd_motu_transaction_unregister(motu); |
8865a31e TS |
134 | snd_card_free(motu->card); |
135 | dev_info(&motu->unit->device, | |
136 | "Sound card registration failed: %d\n", err); | |
137 | } | |
138 | ||
139 | static int motu_probe(struct fw_unit *unit, | |
140 | const struct ieee1394_device_id *entry) | |
141 | { | |
142 | struct snd_motu *motu; | |
143 | ||
144 | /* Allocate this independently of sound card instance. */ | |
145 | motu = kzalloc(sizeof(struct snd_motu), GFP_KERNEL); | |
146 | if (motu == NULL) | |
147 | return -ENOMEM; | |
148 | ||
5e03c33e | 149 | motu->spec = (const struct snd_motu_spec *)entry->driver_data; |
8865a31e | 150 | motu->unit = fw_unit_get(unit); |
6c3cef48 TS |
151 | dev_set_drvdata(&unit->device, motu); |
152 | ||
8865a31e | 153 | mutex_init(&motu->mutex); |
9e796e7d | 154 | spin_lock_init(&motu->lock); |
71c37977 | 155 | init_waitqueue_head(&motu->hwdep_wait); |
8865a31e TS |
156 | |
157 | /* Allocate and register this sound card later. */ | |
158 | INIT_DEFERRABLE_WORK(&motu->dwork, do_registration); | |
159 | snd_fw_schedule_registration(unit, &motu->dwork); | |
160 | ||
6c3cef48 | 161 | return 0; |
6c3cef48 TS |
162 | } |
163 | ||
164 | static void motu_remove(struct fw_unit *unit) | |
165 | { | |
166 | struct snd_motu *motu = dev_get_drvdata(&unit->device); | |
167 | ||
8865a31e TS |
168 | /* |
169 | * Confirm to stop the work for registration before the sound card is | |
170 | * going to be released. The work is not scheduled again because bus | |
171 | * reset handler is not called anymore. | |
172 | */ | |
173 | cancel_delayed_work_sync(&motu->dwork); | |
174 | ||
175 | if (motu->registered) { | |
176 | /* No need to wait for releasing card object in this context. */ | |
177 | snd_card_free_when_closed(motu->card); | |
178 | } else { | |
179 | /* Don't forget this case. */ | |
180 | motu_free(motu); | |
181 | } | |
6c3cef48 TS |
182 | } |
183 | ||
184 | static void motu_bus_update(struct fw_unit *unit) | |
185 | { | |
8865a31e TS |
186 | struct snd_motu *motu = dev_get_drvdata(&unit->device); |
187 | ||
188 | /* Postpone a workqueue for deferred registration. */ | |
189 | if (!motu->registered) | |
190 | snd_fw_schedule_registration(unit, &motu->dwork); | |
2e76701b TS |
191 | |
192 | /* The handler address register becomes initialized. */ | |
193 | snd_motu_transaction_reregister(motu); | |
6c3cef48 TS |
194 | } |
195 | ||
949613e3 TS |
196 | static struct snd_motu_spec motu_828mk2 = { |
197 | .name = "828mk2", | |
198 | .protocol = &snd_motu_protocol_v2, | |
199 | .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | | |
200 | SND_MOTU_SPEC_TX_MICINST_CHUNK | | |
201 | SND_MOTU_SPEC_TX_RETURN_CHUNK | | |
202 | SND_MOTU_SPEC_HAS_OPT_IFACE_A | | |
8b460c76 TS |
203 | SND_MOTU_SPEC_RX_MIDI_2ND_Q | |
204 | SND_MOTU_SPEC_TX_MIDI_2ND_Q, | |
949613e3 TS |
205 | |
206 | .analog_in_ports = 8, | |
207 | .analog_out_ports = 8, | |
208 | }; | |
209 | ||
5992e300 TS |
210 | static struct snd_motu_spec motu_828mk3 = { |
211 | .name = "828mk3", | |
212 | .protocol = &snd_motu_protocol_v3, | |
213 | .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | | |
214 | SND_MOTU_SPEC_SUPPORT_CLOCK_X4 | | |
215 | SND_MOTU_SPEC_TX_MICINST_CHUNK | | |
216 | SND_MOTU_SPEC_TX_RETURN_CHUNK | | |
217 | SND_MOTU_SPEC_TX_REVERB_CHUNK | | |
218 | SND_MOTU_SPEC_HAS_OPT_IFACE_A | | |
219 | SND_MOTU_SPEC_HAS_OPT_IFACE_B | | |
8b460c76 TS |
220 | SND_MOTU_SPEC_RX_MIDI_3RD_Q | |
221 | SND_MOTU_SPEC_TX_MIDI_3RD_Q, | |
5992e300 TS |
222 | |
223 | .analog_in_ports = 8, | |
224 | .analog_out_ports = 8, | |
225 | }; | |
226 | ||
5e03c33e | 227 | #define SND_MOTU_DEV_ENTRY(model, data) \ |
6c3cef48 TS |
228 | { \ |
229 | .match_flags = IEEE1394_MATCH_VENDOR_ID | \ | |
230 | IEEE1394_MATCH_MODEL_ID | \ | |
231 | IEEE1394_MATCH_SPECIFIER_ID, \ | |
232 | .vendor_id = OUI_MOTU, \ | |
233 | .model_id = model, \ | |
234 | .specifier_id = OUI_MOTU, \ | |
5e03c33e | 235 | .driver_data = (kernel_ulong_t)data, \ |
6c3cef48 TS |
236 | } |
237 | ||
238 | static const struct ieee1394_device_id motu_id_table[] = { | |
949613e3 | 239 | SND_MOTU_DEV_ENTRY(0x101800, &motu_828mk2), |
5992e300 TS |
240 | SND_MOTU_DEV_ENTRY(0x106800, &motu_828mk3), /* FireWire only. */ |
241 | SND_MOTU_DEV_ENTRY(0x100800, &motu_828mk3), /* Hybrid. */ | |
6c3cef48 TS |
242 | { } |
243 | }; | |
244 | MODULE_DEVICE_TABLE(ieee1394, motu_id_table); | |
245 | ||
246 | static struct fw_driver motu_driver = { | |
247 | .driver = { | |
248 | .owner = THIS_MODULE, | |
249 | .name = KBUILD_MODNAME, | |
250 | .bus = &fw_bus_type, | |
251 | }, | |
252 | .probe = motu_probe, | |
253 | .update = motu_bus_update, | |
254 | .remove = motu_remove, | |
255 | .id_table = motu_id_table, | |
256 | }; | |
257 | ||
258 | static int __init alsa_motu_init(void) | |
259 | { | |
260 | return driver_register(&motu_driver.driver); | |
261 | } | |
262 | ||
263 | static void __exit alsa_motu_exit(void) | |
264 | { | |
265 | driver_unregister(&motu_driver.driver); | |
266 | } | |
267 | ||
268 | module_init(alsa_motu_init); | |
269 | module_exit(alsa_motu_exit); |