Linux 4.18.10
[linux/fpc-iii.git] / sound / firewire / tascam / tascam.c
blob44ad41fb7374070458de78e991330a744d3f0aff
1 /*
2 * tascam.c - a part of driver for TASCAM FireWire series
4 * Copyright (c) 2015 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
9 #include "tascam.h"
11 MODULE_DESCRIPTION("TASCAM FireWire series Driver");
12 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
13 MODULE_LICENSE("GPL v2");
15 static const struct snd_tscm_spec model_specs[] = {
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,
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,
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,
45 static int identify_model(struct snd_tscm *tscm)
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;
53 if (fw_dev->config_rom_length < 30) {
54 dev_err(&tscm->unit->device,
55 "Configuration ROM is too short.\n");
56 return -ENODEV;
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;
66 model[i] = '\0';
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;
74 if (tscm->spec == NULL)
75 return -ENODEV;
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);
85 return 0;
88 static void tscm_free(struct snd_tscm *tscm)
90 snd_tscm_transaction_unregister(tscm);
91 snd_tscm_stream_destroy_duplex(tscm);
93 fw_unit_put(tscm->unit);
95 mutex_destroy(&tscm->mutex);
98 static void tscm_card_free(struct snd_card *card)
100 tscm_free(card->private_data);
103 static void do_registration(struct work_struct *work)
105 struct snd_tscm *tscm = container_of(work, struct snd_tscm, dwork.work);
106 int err;
108 err = snd_card_new(&tscm->unit->device, -1, NULL, THIS_MODULE, 0,
109 &tscm->card);
110 if (err < 0)
111 return;
113 err = identify_model(tscm);
114 if (err < 0)
115 goto error;
117 err = snd_tscm_transaction_register(tscm);
118 if (err < 0)
119 goto error;
121 err = snd_tscm_stream_init_duplex(tscm);
122 if (err < 0)
123 goto error;
125 snd_tscm_proc_init(tscm);
127 err = snd_tscm_create_pcm_devices(tscm);
128 if (err < 0)
129 goto error;
131 err = snd_tscm_create_midi_devices(tscm);
132 if (err < 0)
133 goto error;
135 err = snd_tscm_create_hwdep_device(tscm);
136 if (err < 0)
137 goto error;
139 err = snd_card_register(tscm->card);
140 if (err < 0)
141 goto error;
144 * After registered, tscm instance can be released corresponding to
145 * releasing the sound card instance.
147 tscm->card->private_free = tscm_card_free;
148 tscm->card->private_data = tscm;
149 tscm->registered = true;
151 return;
152 error:
153 snd_tscm_transaction_unregister(tscm);
154 snd_tscm_stream_destroy_duplex(tscm);
155 snd_card_free(tscm->card);
156 dev_info(&tscm->unit->device,
157 "Sound card registration failed: %d\n", err);
160 static int snd_tscm_probe(struct fw_unit *unit,
161 const struct ieee1394_device_id *entry)
163 struct snd_tscm *tscm;
165 /* Allocate this independent of sound card instance. */
166 tscm = kzalloc(sizeof(struct snd_tscm), GFP_KERNEL);
167 if (tscm == NULL)
168 return -ENOMEM;
170 /* initialize myself */
171 tscm->unit = fw_unit_get(unit);
172 dev_set_drvdata(&unit->device, tscm);
174 mutex_init(&tscm->mutex);
175 spin_lock_init(&tscm->lock);
176 init_waitqueue_head(&tscm->hwdep_wait);
178 /* Allocate and register this sound card later. */
179 INIT_DEFERRABLE_WORK(&tscm->dwork, do_registration);
180 snd_fw_schedule_registration(unit, &tscm->dwork);
182 return 0;
185 static void snd_tscm_update(struct fw_unit *unit)
187 struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
189 /* Postpone a workqueue for deferred registration. */
190 if (!tscm->registered)
191 snd_fw_schedule_registration(unit, &tscm->dwork);
193 snd_tscm_transaction_reregister(tscm);
196 * After registration, userspace can start packet streaming, then this
197 * code block works fine.
199 if (tscm->registered) {
200 mutex_lock(&tscm->mutex);
201 snd_tscm_stream_update_duplex(tscm);
202 mutex_unlock(&tscm->mutex);
206 static void snd_tscm_remove(struct fw_unit *unit)
208 struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
211 * Confirm to stop the work for registration before the sound card is
212 * going to be released. The work is not scheduled again because bus
213 * reset handler is not called anymore.
215 cancel_delayed_work_sync(&tscm->dwork);
217 if (tscm->registered) {
218 /* No need to wait for releasing card object in this context. */
219 snd_card_free_when_closed(tscm->card);
220 } else {
221 /* Don't forget this case. */
222 tscm_free(tscm);
226 static const struct ieee1394_device_id snd_tscm_id_table[] = {
228 .match_flags = IEEE1394_MATCH_VENDOR_ID |
229 IEEE1394_MATCH_SPECIFIER_ID,
230 .vendor_id = 0x00022e,
231 .specifier_id = 0x00022e,
233 /* FE-08 requires reverse-engineering because it just has faders. */
236 MODULE_DEVICE_TABLE(ieee1394, snd_tscm_id_table);
238 static struct fw_driver tscm_driver = {
239 .driver = {
240 .owner = THIS_MODULE,
241 .name = "snd-firewire-tascam",
242 .bus = &fw_bus_type,
244 .probe = snd_tscm_probe,
245 .update = snd_tscm_update,
246 .remove = snd_tscm_remove,
247 .id_table = snd_tscm_id_table,
250 static int __init snd_tscm_init(void)
252 return driver_register(&tscm_driver.driver);
255 static void __exit snd_tscm_exit(void)
257 driver_unregister(&tscm_driver.driver);
260 module_init(snd_tscm_init);
261 module_exit(snd_tscm_exit);