Linux 6.10-rc2
[sfrench/cifs-2.6.git] / sound / firewire / oxfw / oxfw.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * oxfw.c - a part of driver for OXFW970/971 based devices
4  *
5  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6  */
7
8 #include "oxfw.h"
9
10 #define OXFORD_FIRMWARE_ID_ADDRESS      (CSR_REGISTER_BASE + 0x50000)
11 /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
12
13 #define OXFORD_HARDWARE_ID_ADDRESS      (CSR_REGISTER_BASE + 0x90020)
14 #define OXFORD_HARDWARE_ID_OXFW970      0x39443841
15 #define OXFORD_HARDWARE_ID_OXFW971      0x39373100
16
17 #define VENDOR_LOUD             0x000ff2
18 #define VENDOR_GRIFFIN          0x001292
19 #define VENDOR_BEHRINGER        0x001564
20 #define VENDOR_LACIE            0x00d04b
21 #define VENDOR_TASCAM           0x00022e
22 #define OUI_STANTON             0x001260
23 #define OUI_APOGEE              0x0003db
24 #define OUI_OXFORD              0x0030e0
25
26 #define MODEL_SATELLITE         0x00200f
27 #define MODEL_SCS1M             0x001000
28 #define MODEL_DUET_FW           0x01dddd
29 #define MODEL_ONYX_1640I        0x001640
30
31 #define SPECIFIER_1394TA        0x00a02d
32 #define VERSION_AVC             0x010001
33
34 MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
35 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
36 MODULE_LICENSE("GPL");
37 MODULE_ALIAS("snd-firewire-speakers");
38 MODULE_ALIAS("snd-scs1x");
39
40 struct compat_info {
41         const char *driver_name;
42         const char *vendor_name;
43         const char *model_name;
44 };
45
46 static bool detect_loud_models(struct fw_unit *unit)
47 {
48         static const char *const models[] = {
49                 "Onyxi",
50                 "Onyx-i",
51                 "Onyx 1640i",
52                 "d.Pro",
53                 "U.420"};
54         char model[32];
55         int err;
56
57         err = fw_csr_string(unit->directory, CSR_MODEL,
58                             model, sizeof(model));
59         if (err < 0)
60                 return false;
61
62         return match_string(models, ARRAY_SIZE(models), model) >= 0;
63 }
64
65 static int name_card(struct snd_oxfw *oxfw, const struct ieee1394_device_id *entry)
66 {
67         struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
68         const struct compat_info *info;
69         char vendor[24];
70         char model[32];
71         const char *d, *v, *m;
72         u32 firmware;
73         int err;
74
75         /* get vendor name from root directory */
76         err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
77                             vendor, sizeof(vendor));
78         if (err < 0)
79                 goto end;
80
81         /* get model name from unit directory */
82         err = fw_csr_string(oxfw->unit->directory, CSR_MODEL,
83                             model, sizeof(model));
84         if (err < 0)
85                 goto end;
86
87         err = snd_fw_transaction(oxfw->unit, TCODE_READ_QUADLET_REQUEST,
88                                  OXFORD_FIRMWARE_ID_ADDRESS, &firmware, 4, 0);
89         if (err < 0)
90                 goto end;
91         be32_to_cpus(&firmware);
92
93         if (firmware >> 20 == 0x970)
94                 oxfw->quirks |= SND_OXFW_QUIRK_JUMBO_PAYLOAD;
95
96         /* to apply card definitions */
97         if (entry->vendor_id == VENDOR_GRIFFIN || entry->vendor_id == VENDOR_LACIE) {
98                 info = (const struct compat_info *)entry->driver_data;
99                 d = info->driver_name;
100                 v = info->vendor_name;
101                 m = info->model_name;
102         } else {
103                 d = "OXFW";
104                 v = vendor;
105                 m = model;
106         }
107
108         strcpy(oxfw->card->driver, d);
109         strcpy(oxfw->card->mixername, m);
110         strcpy(oxfw->card->shortname, m);
111
112         scnprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
113                   "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
114                   v, m, firmware >> 20, firmware & 0xffff,
115                   fw_dev->config_rom[3], fw_dev->config_rom[4],
116                   dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
117 end:
118         return err;
119 }
120
121 static void oxfw_card_free(struct snd_card *card)
122 {
123         struct snd_oxfw *oxfw = card->private_data;
124
125         if (oxfw->has_output || oxfw->has_input)
126                 snd_oxfw_stream_destroy_duplex(oxfw);
127
128         mutex_destroy(&oxfw->mutex);
129         fw_unit_put(oxfw->unit);
130 }
131
132 static int detect_quirks(struct snd_oxfw *oxfw, const struct ieee1394_device_id *entry)
133 {
134         struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
135         struct fw_csr_iterator it;
136         int key, val;
137         int vendor, model;
138
139         /*
140          * Add ALSA control elements for two models to keep compatibility to
141          * old firewire-speaker module.
142          */
143         if (entry->vendor_id == VENDOR_GRIFFIN)
144                 return snd_oxfw_add_spkr(oxfw, false);
145         if (entry->vendor_id == VENDOR_LACIE)
146                 return snd_oxfw_add_spkr(oxfw, true);
147
148         /*
149          * Stanton models supports asynchronous transactions for unique MIDI
150          * messages.
151          */
152         if (entry->vendor_id == OUI_STANTON) {
153                 oxfw->quirks |= SND_OXFW_QUIRK_SCS_TRANSACTION;
154                 if (entry->model_id == MODEL_SCS1M)
155                         oxfw->quirks |= SND_OXFW_QUIRK_BLOCKING_TRANSMISSION;
156
157                 // No physical MIDI ports.
158                 oxfw->midi_input_ports = 0;
159                 oxfw->midi_output_ports = 0;
160
161                 return snd_oxfw_scs1x_add(oxfw);
162         }
163
164         if (entry->vendor_id == OUI_APOGEE && entry->model_id == MODEL_DUET_FW) {
165                 oxfw->quirks |= SND_OXFW_QUIRK_BLOCKING_TRANSMISSION |
166                                 SND_OXFW_QUIRK_IGNORE_NO_INFO_PACKET;
167         }
168
169         /*
170          * TASCAM FireOne has physical control and requires a pair of additional
171          * MIDI ports.
172          */
173         if (entry->vendor_id == VENDOR_TASCAM) {
174                 oxfw->midi_input_ports++;
175                 oxfw->midi_output_ports++;
176                 return 0;
177         }
178
179         /* Seek from Root Directory of Config ROM. */
180         vendor = model = 0;
181         fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
182         while (fw_csr_iterator_next(&it, &key, &val)) {
183                 if (key == CSR_VENDOR)
184                         vendor = val;
185                 else if (key == CSR_MODEL)
186                         model = val;
187         }
188
189         if (vendor == VENDOR_LOUD) {
190                 // Mackie Onyx Satellite with base station has a quirk to report a wrong
191                 // value in 'dbs' field of CIP header against its format information.
192                 oxfw->quirks |= SND_OXFW_QUIRK_WRONG_DBS;
193
194                 // OXFW971-based models may transfer events by blocking method.
195                 if (!(oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD))
196                         oxfw->quirks |= SND_OXFW_QUIRK_BLOCKING_TRANSMISSION;
197
198                 if (model == MODEL_ONYX_1640I) {
199                         //Unless receiving packets without NOINFO packet, the device transfers
200                         //mostly half of events in packets than expected.
201                         oxfw->quirks |= SND_OXFW_QUIRK_IGNORE_NO_INFO_PACKET |
202                                         SND_OXFW_QUIRK_VOLUNTARY_RECOVERY;
203                 }
204         }
205
206         return 0;
207 }
208
209 static int oxfw_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
210 {
211         struct snd_card *card;
212         struct snd_oxfw *oxfw;
213         int err;
214
215         if (entry->vendor_id == VENDOR_LOUD && entry->model_id == 0 && !detect_loud_models(unit))
216                 return -ENODEV;
217
218         err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, sizeof(*oxfw), &card);
219         if (err < 0)
220                 return err;
221         card->private_free = oxfw_card_free;
222
223         oxfw = card->private_data;
224         oxfw->unit = fw_unit_get(unit);
225         dev_set_drvdata(&unit->device, oxfw);
226         oxfw->card = card;
227
228         mutex_init(&oxfw->mutex);
229         spin_lock_init(&oxfw->lock);
230         init_waitqueue_head(&oxfw->hwdep_wait);
231
232         err = name_card(oxfw, entry);
233         if (err < 0)
234                 goto error;
235
236         if (entry->vendor_id == OUI_OXFORD && entry->model_id == 0x00f970) {
237                 oxfw->quirks |= SND_OXFW_QUIRK_STREAM_FORMAT_INFO_UNSUPPORTED |
238                                 SND_OXFW_QUIRK_DBC_IS_TOTAL_PAYLOAD_QUADLETS;
239         }
240
241         err = snd_oxfw_stream_discover(oxfw);
242         if (err < 0)
243                 goto error;
244
245         err = detect_quirks(oxfw, entry);
246         if (err < 0)
247                 goto error;
248
249         if (oxfw->has_output || oxfw->has_input) {
250                 err = snd_oxfw_stream_init_duplex(oxfw);
251                 if (err < 0)
252                         goto error;
253
254                 err = snd_oxfw_create_pcm(oxfw);
255                 if (err < 0)
256                         goto error;
257
258                 snd_oxfw_proc_init(oxfw);
259
260                 err = snd_oxfw_create_midi(oxfw);
261                 if (err < 0)
262                         goto error;
263
264                 err = snd_oxfw_create_hwdep(oxfw);
265                 if (err < 0)
266                         goto error;
267         }
268
269         err = snd_card_register(card);
270         if (err < 0)
271                 goto error;
272
273         return 0;
274 error:
275         snd_card_free(card);
276         return err;
277 }
278
279 static void oxfw_bus_reset(struct fw_unit *unit)
280 {
281         struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
282
283         fcp_bus_reset(oxfw->unit);
284
285         if (oxfw->has_output || oxfw->has_input) {
286                 mutex_lock(&oxfw->mutex);
287                 snd_oxfw_stream_update_duplex(oxfw);
288                 mutex_unlock(&oxfw->mutex);
289         }
290
291         if (oxfw->quirks & SND_OXFW_QUIRK_SCS_TRANSACTION)
292                 snd_oxfw_scs1x_update(oxfw);
293 }
294
295 static void oxfw_remove(struct fw_unit *unit)
296 {
297         struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
298
299         // Block till all of ALSA character devices are released.
300         snd_card_free(oxfw->card);
301 }
302
303 static const struct compat_info griffin_firewave = {
304         .driver_name = "FireWave",
305         .vendor_name = "Griffin",
306         .model_name = "FireWave",
307 };
308
309 static const struct compat_info lacie_speakers = {
310         .driver_name = "FWSpeakers",
311         .vendor_name = "LaCie",
312         .model_name = "FireWire Speakers",
313 };
314
315 #define OXFW_DEV_ENTRY(vendor, model, data) \
316 { \
317         .match_flags  = IEEE1394_MATCH_VENDOR_ID | \
318                         IEEE1394_MATCH_MODEL_ID | \
319                         IEEE1394_MATCH_SPECIFIER_ID | \
320                         IEEE1394_MATCH_VERSION, \
321         .vendor_id    = vendor, \
322         .model_id     = model, \
323         .specifier_id = SPECIFIER_1394TA, \
324         .version      = VERSION_AVC, \
325         .driver_data  = (kernel_ulong_t)data, \
326 }
327
328 static const struct ieee1394_device_id oxfw_id_table[] = {
329         //
330         // OXFW970 devices:
331         // Initial firmware has a quirk to postpone isoc packet transmission during finishing async
332         // transaction. As a result, several isochronous cycles are skipped to transfer the packets
333         // and the audio data frames which should have been transferred during the cycles are put
334         // into packet at the first isoc cycle after the postpone. Furthermore, the value of SYT
335         // field in CIP header is not reliable as synchronization timing,
336         //
337         OXFW_DEV_ENTRY(VENDOR_GRIFFIN, 0x00f970, &griffin_firewave),
338         OXFW_DEV_ENTRY(VENDOR_LACIE, 0x00f970, &lacie_speakers),
339         // Miglia HarmonyAudio (HA02). The numeric vendor ID is ASIC vendor and the model ID is the
340         // default value of ASIC.
341         OXFW_DEV_ENTRY(OUI_OXFORD, 0x00f970, NULL),
342         // Behringer,F-Control Audio 202. The value of SYT field is not reliable at all.
343         OXFW_DEV_ENTRY(VENDOR_BEHRINGER, 0x00fc22, NULL),
344         // Loud Technologies, Tapco Link.FireWire 4x6. The value of SYT field is always 0xffff.
345         OXFW_DEV_ENTRY(VENDOR_LOUD, 0x000460, NULL),
346         // Loud Technologies, Mackie Onyx Satellite. Although revised version of firmware is
347         // installed to avoid the postpone, the value of SYT field is always 0xffff.
348         OXFW_DEV_ENTRY(VENDOR_LOUD, MODEL_SATELLITE, NULL),
349
350         //
351         // OXFW971 devices:
352         // The value of SYT field in CIP header is enough reliable. Both of blocking and non-blocking
353         // transmission methods are available.
354         //
355         // Any Mackie(Loud) models (name string/model id):
356         //  Onyx-i series (former models):      0x081216
357         //  Onyx 1640i:                         0x001640
358         //  d.2 pro/d.4 pro (built-in card):    Unknown
359         //  U.420:                              Unknown
360         //  U.420d:                             Unknown
361         {
362                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
363                                   IEEE1394_MATCH_SPECIFIER_ID |
364                                   IEEE1394_MATCH_VERSION,
365                 .vendor_id      = VENDOR_LOUD,
366                 .model_id       = 0,
367                 .specifier_id   = SPECIFIER_1394TA,
368                 .version        = VERSION_AVC,
369         },
370         // TASCAM, FireOne.
371         OXFW_DEV_ENTRY(VENDOR_TASCAM, 0x800007, NULL),
372         // Stanton, Stanton Controllers & Systems 1 Mixer (SCS.1m).
373         OXFW_DEV_ENTRY(OUI_STANTON, MODEL_SCS1M, NULL),
374         // Stanton, Stanton Controllers & Systems 1 Deck (SCS.1d).
375         OXFW_DEV_ENTRY(OUI_STANTON, 0x002000, NULL),
376         // APOGEE, duet FireWire.
377         OXFW_DEV_ENTRY(OUI_APOGEE, MODEL_DUET_FW, NULL),
378         { }
379 };
380 MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
381
382 static struct fw_driver oxfw_driver = {
383         .driver   = {
384                 .owner  = THIS_MODULE,
385                 .name   = KBUILD_MODNAME,
386                 .bus    = &fw_bus_type,
387         },
388         .probe    = oxfw_probe,
389         .update   = oxfw_bus_reset,
390         .remove   = oxfw_remove,
391         .id_table = oxfw_id_table,
392 };
393
394 static int __init snd_oxfw_init(void)
395 {
396         return driver_register(&oxfw_driver.driver);
397 }
398
399 static void __exit snd_oxfw_exit(void)
400 {
401         driver_unregister(&oxfw_driver.driver);
402 }
403
404 module_init(snd_oxfw_init);
405 module_exit(snd_oxfw_exit);