]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/firewire/motu/motu.c
ALSA: firewire-motu: handle transactions specific for MOTU FireWire models
[mirror_ubuntu-bionic-kernel.git] / sound / firewire / motu / motu.c
CommitLineData
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
13MODULE_DESCRIPTION("MOTU FireWire driver");
14MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
15MODULE_LICENSE("GPL v2");
16
59f6482c
TS
17const 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
29static 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 55static void motu_free(struct snd_motu *motu)
6c3cef48 56{
2e76701b
TS
57 snd_motu_transaction_unregister(motu);
58
6c3cef48
TS
59 fw_unit_put(motu->unit);
60
61 mutex_destroy(&motu->mutex);
8865a31e 62 kfree(motu);
6c3cef48
TS
63}
64
8865a31e
TS
65/*
66 * This module releases the FireWire unit data after all ALSA character devices
67 * are released by applications. This is for releasing stream data or finishing
68 * transactions safely. Thus at returning from .remove(), this module still keep
69 * references for the unit.
70 */
71static void motu_card_free(struct snd_card *card)
6c3cef48 72{
8865a31e
TS
73 motu_free(card->private_data);
74}
6c3cef48 75
8865a31e
TS
76static void do_registration(struct work_struct *work)
77{
78 struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work);
79 int err;
6c3cef48 80
8865a31e
TS
81 if (motu->registered)
82 return;
6c3cef48 83
8865a31e
TS
84 err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
85 &motu->card);
86 if (err < 0)
87 return;
6c3cef48
TS
88
89 name_card(motu);
90
2e76701b
TS
91 err = snd_motu_transaction_register(motu);
92 if (err < 0)
93 goto error;
94
8865a31e 95 err = snd_card_register(motu->card);
6c3cef48
TS
96 if (err < 0)
97 goto error;
98
8865a31e
TS
99 /*
100 * After registered, motu instance can be released corresponding to
101 * releasing the sound card instance.
102 */
103 motu->card->private_free = motu_card_free;
104 motu->card->private_data = motu;
105 motu->registered = true;
106
107 return;
108error:
2e76701b 109 snd_motu_transaction_unregister(motu);
8865a31e
TS
110 snd_card_free(motu->card);
111 dev_info(&motu->unit->device,
112 "Sound card registration failed: %d\n", err);
113}
114
115static int motu_probe(struct fw_unit *unit,
116 const struct ieee1394_device_id *entry)
117{
118 struct snd_motu *motu;
119
120 /* Allocate this independently of sound card instance. */
121 motu = kzalloc(sizeof(struct snd_motu), GFP_KERNEL);
122 if (motu == NULL)
123 return -ENOMEM;
124
5e03c33e 125 motu->spec = (const struct snd_motu_spec *)entry->driver_data;
8865a31e 126 motu->unit = fw_unit_get(unit);
6c3cef48
TS
127 dev_set_drvdata(&unit->device, motu);
128
8865a31e
TS
129 mutex_init(&motu->mutex);
130
131 /* Allocate and register this sound card later. */
132 INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
133 snd_fw_schedule_registration(unit, &motu->dwork);
134
6c3cef48 135 return 0;
6c3cef48
TS
136}
137
138static void motu_remove(struct fw_unit *unit)
139{
140 struct snd_motu *motu = dev_get_drvdata(&unit->device);
141
8865a31e
TS
142 /*
143 * Confirm to stop the work for registration before the sound card is
144 * going to be released. The work is not scheduled again because bus
145 * reset handler is not called anymore.
146 */
147 cancel_delayed_work_sync(&motu->dwork);
148
149 if (motu->registered) {
150 /* No need to wait for releasing card object in this context. */
151 snd_card_free_when_closed(motu->card);
152 } else {
153 /* Don't forget this case. */
154 motu_free(motu);
155 }
6c3cef48
TS
156}
157
158static void motu_bus_update(struct fw_unit *unit)
159{
8865a31e
TS
160 struct snd_motu *motu = dev_get_drvdata(&unit->device);
161
162 /* Postpone a workqueue for deferred registration. */
163 if (!motu->registered)
164 snd_fw_schedule_registration(unit, &motu->dwork);
2e76701b
TS
165
166 /* The handler address register becomes initialized. */
167 snd_motu_transaction_reregister(motu);
6c3cef48
TS
168}
169
5e03c33e 170#define SND_MOTU_DEV_ENTRY(model, data) \
6c3cef48
TS
171{ \
172 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
173 IEEE1394_MATCH_MODEL_ID | \
174 IEEE1394_MATCH_SPECIFIER_ID, \
175 .vendor_id = OUI_MOTU, \
176 .model_id = model, \
177 .specifier_id = OUI_MOTU, \
5e03c33e 178 .driver_data = (kernel_ulong_t)data, \
6c3cef48
TS
179}
180
181static const struct ieee1394_device_id motu_id_table[] = {
182 { }
183};
184MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
185
186static struct fw_driver motu_driver = {
187 .driver = {
188 .owner = THIS_MODULE,
189 .name = KBUILD_MODNAME,
190 .bus = &fw_bus_type,
191 },
192 .probe = motu_probe,
193 .update = motu_bus_update,
194 .remove = motu_remove,
195 .id_table = motu_id_table,
196};
197
198static int __init alsa_motu_init(void)
199{
200 return driver_register(&motu_driver.driver);
201}
202
203static void __exit alsa_motu_exit(void)
204{
205 driver_unregister(&motu_driver.driver);
206}
207
208module_init(alsa_motu_init);
209module_exit(alsa_motu_exit);