GNU Linux-libre 4.19.264-gnu1
[releases.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         // Tascam, FW-1884.
229         {
230                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
231                                IEEE1394_MATCH_SPECIFIER_ID |
232                                IEEE1394_MATCH_VERSION,
233                 .vendor_id = 0x00022e,
234                 .specifier_id = 0x00022e,
235                 .version = 0x800000,
236         },
237         // Tascam, FE-8 (.version = 0x800001)
238         // This kernel module doesn't support FE-8 because the most of features
239         // can be implemented in userspace without any specific support of this
240         // module.
241         //
242         // .version = 0x800002 is unknown.
243         //
244         // Tascam, FW-1082.
245         {
246                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
247                                IEEE1394_MATCH_SPECIFIER_ID |
248                                IEEE1394_MATCH_VERSION,
249                 .vendor_id = 0x00022e,
250                 .specifier_id = 0x00022e,
251                 .version = 0x800003,
252         },
253         // Tascam, FW-1804.
254         {
255                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
256                                IEEE1394_MATCH_SPECIFIER_ID |
257                                IEEE1394_MATCH_VERSION,
258                 .vendor_id = 0x00022e,
259                 .specifier_id = 0x00022e,
260                 .version = 0x800004,
261         },
262         /* FE-08 requires reverse-engineering because it just has faders. */
263         {}
264 };
265 MODULE_DEVICE_TABLE(ieee1394, snd_tscm_id_table);
266
267 static struct fw_driver tscm_driver = {
268         .driver = {
269                 .owner = THIS_MODULE,
270                 .name = "snd-firewire-tascam",
271                 .bus = &fw_bus_type,
272         },
273         .probe    = snd_tscm_probe,
274         .update   = snd_tscm_update,
275         .remove   = snd_tscm_remove,
276         .id_table = snd_tscm_id_table,
277 };
278
279 static int __init snd_tscm_init(void)
280 {
281         return driver_register(&tscm_driver.driver);
282 }
283
284 static void __exit snd_tscm_exit(void)
285 {
286         driver_unregister(&tscm_driver.driver);
287 }
288
289 module_init(snd_tscm_init);
290 module_exit(snd_tscm_exit);