]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/firewire/motu/motu.c
Merge tag 'mvebu-fixes-4.13-3' of git://git.infradead.org/linux-mvebu into fixes
[karo-tx-linux.git] / sound / firewire / motu / motu.c
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
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
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");
46         strcpy(motu->card->shortname, motu->spec->name);
47         strcpy(motu->card->mixername, motu->spec->name);
48         snprintf(motu->card->longname, sizeof(motu->card->longname),
49                  "MOTU %s (version:%d), GUID %08x%08x at %s, S%d",
50                  motu->spec->name, version,
51                  fw_dev->config_rom[3], fw_dev->config_rom[4],
52                  dev_name(&motu->unit->device), 100 << fw_dev->max_speed);
53 }
54
55 static void motu_free(struct snd_motu *motu)
56 {
57         snd_motu_transaction_unregister(motu);
58
59         snd_motu_stream_destroy_duplex(motu);
60         fw_unit_put(motu->unit);
61
62         mutex_destroy(&motu->mutex);
63         kfree(motu);
64 }
65
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)
73 {
74         motu_free(card->private_data);
75 }
76
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;
81
82         if (motu->registered)
83                 return;
84
85         err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
86                            &motu->card);
87         if (err < 0)
88                 return;
89
90         name_card(motu);
91
92         err = snd_motu_transaction_register(motu);
93         if (err < 0)
94                 goto error;
95
96         err = snd_motu_stream_init_duplex(motu);
97         if (err < 0)
98                 goto error;
99
100         snd_motu_proc_init(motu);
101
102         err = snd_motu_create_pcm_devices(motu);
103         if (err < 0)
104                 goto error;
105
106         if (motu->spec->flags & SND_MOTU_SPEC_HAS_MIDI) {
107                 err = snd_motu_create_midi_devices(motu);
108                 if (err < 0)
109                         goto error;
110         }
111
112         err = snd_motu_create_hwdep_device(motu);
113         if (err < 0)
114                 goto error;
115
116         err = snd_card_register(motu->card);
117         if (err < 0)
118                 goto error;
119
120         /*
121          * After registered, motu instance can be released corresponding to
122          * releasing the sound card instance.
123          */
124         motu->card->private_free = motu_card_free;
125         motu->card->private_data = motu;
126         motu->registered = true;
127
128         return;
129 error:
130         snd_motu_transaction_unregister(motu);
131         snd_card_free(motu->card);
132         dev_info(&motu->unit->device,
133                  "Sound card registration failed: %d\n", err);
134 }
135
136 static int motu_probe(struct fw_unit *unit,
137                       const struct ieee1394_device_id *entry)
138 {
139         struct snd_motu *motu;
140
141         /* Allocate this independently of sound card instance. */
142         motu = kzalloc(sizeof(struct snd_motu), GFP_KERNEL);
143         if (motu == NULL)
144                 return -ENOMEM;
145
146         motu->spec = (const struct snd_motu_spec *)entry->driver_data;
147         motu->unit = fw_unit_get(unit);
148         dev_set_drvdata(&unit->device, motu);
149
150         mutex_init(&motu->mutex);
151         spin_lock_init(&motu->lock);
152         init_waitqueue_head(&motu->hwdep_wait);
153
154         /* Allocate and register this sound card later. */
155         INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
156         snd_fw_schedule_registration(unit, &motu->dwork);
157
158         return 0;
159 }
160
161 static void motu_remove(struct fw_unit *unit)
162 {
163         struct snd_motu *motu = dev_get_drvdata(&unit->device);
164
165         /*
166          * Confirm to stop the work for registration before the sound card is
167          * going to be released. The work is not scheduled again because bus
168          * reset handler is not called anymore.
169          */
170         cancel_delayed_work_sync(&motu->dwork);
171
172         if (motu->registered) {
173                 /* No need to wait for releasing card object in this context. */
174                 snd_card_free_when_closed(motu->card);
175         } else {
176                 /* Don't forget this case. */
177                 motu_free(motu);
178         }
179 }
180
181 static void motu_bus_update(struct fw_unit *unit)
182 {
183         struct snd_motu *motu = dev_get_drvdata(&unit->device);
184
185         /* Postpone a workqueue for deferred registration. */
186         if (!motu->registered)
187                 snd_fw_schedule_registration(unit, &motu->dwork);
188
189         /* The handler address register becomes initialized. */
190         snd_motu_transaction_reregister(motu);
191 }
192
193 static struct snd_motu_spec motu_828mk2 = {
194         .name = "828mk2",
195         .protocol = &snd_motu_protocol_v2,
196         .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
197                  SND_MOTU_SPEC_TX_MICINST_CHUNK |
198                  SND_MOTU_SPEC_TX_RETURN_CHUNK |
199                  SND_MOTU_SPEC_HAS_OPT_IFACE_A |
200                  SND_MOTU_SPEC_HAS_MIDI,
201
202         .analog_in_ports = 8,
203         .analog_out_ports = 8,
204 };
205
206 static struct snd_motu_spec motu_828mk3 = {
207         .name = "828mk3",
208         .protocol = &snd_motu_protocol_v3,
209         .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
210                  SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
211                  SND_MOTU_SPEC_TX_MICINST_CHUNK |
212                  SND_MOTU_SPEC_TX_RETURN_CHUNK |
213                  SND_MOTU_SPEC_TX_REVERB_CHUNK |
214                  SND_MOTU_SPEC_HAS_OPT_IFACE_A |
215                  SND_MOTU_SPEC_HAS_OPT_IFACE_B |
216                  SND_MOTU_SPEC_HAS_MIDI,
217
218         .analog_in_ports = 8,
219         .analog_out_ports = 8,
220 };
221
222 #define SND_MOTU_DEV_ENTRY(model, data)                 \
223 {                                                       \
224         .match_flags    = IEEE1394_MATCH_VENDOR_ID |    \
225                           IEEE1394_MATCH_MODEL_ID |     \
226                           IEEE1394_MATCH_SPECIFIER_ID,  \
227         .vendor_id      = OUI_MOTU,                     \
228         .model_id       = model,                        \
229         .specifier_id   = OUI_MOTU,                     \
230         .driver_data    = (kernel_ulong_t)data,         \
231 }
232
233 static const struct ieee1394_device_id motu_id_table[] = {
234         SND_MOTU_DEV_ENTRY(0x101800, &motu_828mk2),
235         SND_MOTU_DEV_ENTRY(0x106800, &motu_828mk3),     /* FireWire only. */
236         SND_MOTU_DEV_ENTRY(0x100800, &motu_828mk3),     /* Hybrid. */
237         { }
238 };
239 MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
240
241 static struct fw_driver motu_driver = {
242         .driver   = {
243                 .owner  = THIS_MODULE,
244                 .name   = KBUILD_MODNAME,
245                 .bus    = &fw_bus_type,
246         },
247         .probe    = motu_probe,
248         .update   = motu_bus_update,
249         .remove   = motu_remove,
250         .id_table = motu_id_table,
251 };
252
253 static int __init alsa_motu_init(void)
254 {
255         return driver_register(&motu_driver.driver);
256 }
257
258 static void __exit alsa_motu_exit(void)
259 {
260         driver_unregister(&motu_driver.driver);
261 }
262
263 module_init(alsa_motu_init);
264 module_exit(alsa_motu_exit);