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