]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/firewire/tascam/tascam.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / sound / firewire / tascam / tascam.c
1 /*
2 * tascam.c - a part of driver for TASCAM FireWire series
3 *
4 * Copyright (c) 2015 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9 #include "tascam.h"
10
11 MODULE_DESCRIPTION("TASCAM FireWire series Driver");
12 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
13 MODULE_LICENSE("GPL v2");
14
15 static const struct snd_tscm_spec model_specs[] = {
16 {
17 .name = "FW-1884",
18 .has_adat = true,
19 .has_spdif = true,
20 .pcm_capture_analog_channels = 8,
21 .pcm_playback_analog_channels = 8,
22 .midi_capture_ports = 4,
23 .midi_playback_ports = 4,
24 },
25 {
26 .name = "FW-1082",
27 .has_adat = false,
28 .has_spdif = true,
29 .pcm_capture_analog_channels = 8,
30 .pcm_playback_analog_channels = 2,
31 .midi_capture_ports = 2,
32 .midi_playback_ports = 2,
33 },
34 {
35 .name = "FW-1804",
36 .has_adat = true,
37 .has_spdif = true,
38 .pcm_capture_analog_channels = 8,
39 .pcm_playback_analog_channels = 2,
40 .midi_capture_ports = 2,
41 .midi_playback_ports = 4,
42 },
43 };
44
45 static int identify_model(struct snd_tscm *tscm)
46 {
47 struct fw_device *fw_dev = fw_parent_device(tscm->unit);
48 const u32 *config_rom = fw_dev->config_rom;
49 char model[9];
50 unsigned int i;
51 u8 c;
52
53 if (fw_dev->config_rom_length < 30) {
54 dev_err(&tscm->unit->device,
55 "Configuration ROM is too short.\n");
56 return -ENODEV;
57 }
58
59 /* Pick up model name from certain addresses. */
60 for (i = 0; i < 8; i++) {
61 c = config_rom[28 + i / 4] >> (24 - 8 * (i % 4));
62 if (c == '\0')
63 break;
64 model[i] = c;
65 }
66 model[i] = '\0';
67
68 for (i = 0; i < ARRAY_SIZE(model_specs); i++) {
69 if (strcmp(model, model_specs[i].name) == 0) {
70 tscm->spec = &model_specs[i];
71 break;
72 }
73 }
74 if (tscm->spec == NULL)
75 return -ENODEV;
76
77 strcpy(tscm->card->driver, "FW-TASCAM");
78 strcpy(tscm->card->shortname, model);
79 strcpy(tscm->card->mixername, model);
80 snprintf(tscm->card->longname, sizeof(tscm->card->longname),
81 "TASCAM %s, GUID %08x%08x at %s, S%d", model,
82 fw_dev->config_rom[3], fw_dev->config_rom[4],
83 dev_name(&tscm->unit->device), 100 << fw_dev->max_speed);
84
85 return 0;
86 }
87
88 static void tscm_free(struct snd_tscm *tscm)
89 {
90 snd_tscm_transaction_unregister(tscm);
91 snd_tscm_stream_destroy_duplex(tscm);
92
93 fw_unit_put(tscm->unit);
94
95 mutex_destroy(&tscm->mutex);
96 kfree(tscm);
97 }
98
99 static void tscm_card_free(struct snd_card *card)
100 {
101 tscm_free(card->private_data);
102 }
103
104 static void do_registration(struct work_struct *work)
105 {
106 struct snd_tscm *tscm = container_of(work, struct snd_tscm, dwork.work);
107 int err;
108
109 err = snd_card_new(&tscm->unit->device, -1, NULL, THIS_MODULE, 0,
110 &tscm->card);
111 if (err < 0)
112 return;
113
114 err = identify_model(tscm);
115 if (err < 0)
116 goto error;
117
118 err = snd_tscm_transaction_register(tscm);
119 if (err < 0)
120 goto error;
121
122 err = snd_tscm_stream_init_duplex(tscm);
123 if (err < 0)
124 goto error;
125
126 snd_tscm_proc_init(tscm);
127
128 err = snd_tscm_create_pcm_devices(tscm);
129 if (err < 0)
130 goto error;
131
132 err = snd_tscm_create_midi_devices(tscm);
133 if (err < 0)
134 goto error;
135
136 err = snd_tscm_create_hwdep_device(tscm);
137 if (err < 0)
138 goto error;
139
140 err = snd_card_register(tscm->card);
141 if (err < 0)
142 goto error;
143
144 /*
145 * After registered, tscm instance can be released corresponding to
146 * releasing the sound card instance.
147 */
148 tscm->card->private_free = tscm_card_free;
149 tscm->card->private_data = tscm;
150 tscm->registered = true;
151
152 return;
153 error:
154 snd_tscm_transaction_unregister(tscm);
155 snd_tscm_stream_destroy_duplex(tscm);
156 snd_card_free(tscm->card);
157 dev_info(&tscm->unit->device,
158 "Sound card registration failed: %d\n", err);
159 }
160
161 static int snd_tscm_probe(struct fw_unit *unit,
162 const struct ieee1394_device_id *entry)
163 {
164 struct snd_tscm *tscm;
165
166 /* Allocate this independent of sound card instance. */
167 tscm = kzalloc(sizeof(struct snd_tscm), GFP_KERNEL);
168 if (tscm == NULL)
169 return -ENOMEM;
170
171 /* initialize myself */
172 tscm->unit = fw_unit_get(unit);
173 dev_set_drvdata(&unit->device, tscm);
174
175 mutex_init(&tscm->mutex);
176 spin_lock_init(&tscm->lock);
177 init_waitqueue_head(&tscm->hwdep_wait);
178
179 /* Allocate and register this sound card later. */
180 INIT_DEFERRABLE_WORK(&tscm->dwork, do_registration);
181 snd_fw_schedule_registration(unit, &tscm->dwork);
182
183 return 0;
184 }
185
186 static void snd_tscm_update(struct fw_unit *unit)
187 {
188 struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
189
190 /* Postpone a workqueue for deferred registration. */
191 if (!tscm->registered)
192 snd_fw_schedule_registration(unit, &tscm->dwork);
193
194 snd_tscm_transaction_reregister(tscm);
195
196 /*
197 * After registration, userspace can start packet streaming, then this
198 * code block works fine.
199 */
200 if (tscm->registered) {
201 mutex_lock(&tscm->mutex);
202 snd_tscm_stream_update_duplex(tscm);
203 mutex_unlock(&tscm->mutex);
204 }
205 }
206
207 static void snd_tscm_remove(struct fw_unit *unit)
208 {
209 struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
210
211 /*
212 * Confirm to stop the work for registration before the sound card is
213 * going to be released. The work is not scheduled again because bus
214 * reset handler is not called anymore.
215 */
216 cancel_delayed_work_sync(&tscm->dwork);
217
218 if (tscm->registered) {
219 /* No need to wait for releasing card object in this context. */
220 snd_card_free_when_closed(tscm->card);
221 } else {
222 /* Don't forget this case. */
223 tscm_free(tscm);
224 }
225 }
226
227 static const struct ieee1394_device_id snd_tscm_id_table[] = {
228 {
229 .match_flags = IEEE1394_MATCH_VENDOR_ID |
230 IEEE1394_MATCH_SPECIFIER_ID,
231 .vendor_id = 0x00022e,
232 .specifier_id = 0x00022e,
233 },
234 /* FE-08 requires reverse-engineering because it just has faders. */
235 {}
236 };
237 MODULE_DEVICE_TABLE(ieee1394, snd_tscm_id_table);
238
239 static struct fw_driver tscm_driver = {
240 .driver = {
241 .owner = THIS_MODULE,
242 .name = "snd-firewire-tascam",
243 .bus = &fw_bus_type,
244 },
245 .probe = snd_tscm_probe,
246 .update = snd_tscm_update,
247 .remove = snd_tscm_remove,
248 .id_table = snd_tscm_id_table,
249 };
250
251 static int __init snd_tscm_init(void)
252 {
253 return driver_register(&tscm_driver.driver);
254 }
255
256 static void __exit snd_tscm_exit(void)
257 {
258 driver_unregister(&tscm_driver.driver);
259 }
260
261 module_init(snd_tscm_init);
262 module_exit(snd_tscm_exit);