Merge tag 'pinctrl-v5.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[sfrench/cifs-2.6.git] / sound / firewire / bebob / bebob.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * bebob.c - a part of driver for BeBoB based devices
4  *
5  * Copyright (c) 2013-2014 Takashi Sakamoto
6  */
7
8 /*
9  * BeBoB is 'BridgeCo enhanced Breakout Box'. This is installed to firewire
10  * devices with DM1000/DM1100/DM1500 chipset. It gives common way for host
11  * system to handle BeBoB based devices.
12  */
13
14 #include "bebob.h"
15
16 MODULE_DESCRIPTION("BridgeCo BeBoB driver");
17 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
18 MODULE_LICENSE("GPL v2");
19
20 static int index[SNDRV_CARDS]   = SNDRV_DEFAULT_IDX;
21 static char *id[SNDRV_CARDS]    = SNDRV_DEFAULT_STR;
22 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
23
24 module_param_array(index, int, NULL, 0444);
25 MODULE_PARM_DESC(index, "card index");
26 module_param_array(id, charp, NULL, 0444);
27 MODULE_PARM_DESC(id, "ID string");
28 module_param_array(enable, bool, NULL, 0444);
29 MODULE_PARM_DESC(enable, "enable BeBoB sound card");
30
31 static DEFINE_MUTEX(devices_mutex);
32 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
33
34 /* Offsets from information register. */
35 #define INFO_OFFSET_BEBOB_VERSION       0x08
36 #define INFO_OFFSET_GUID                0x10
37 #define INFO_OFFSET_HW_MODEL_ID         0x18
38 #define INFO_OFFSET_HW_MODEL_REVISION   0x1c
39
40 #define VEN_EDIROL      0x000040ab
41 #define VEN_PRESONUS    0x00000a92
42 #define VEN_BRIDGECO    0x000007f5
43 #define VEN_MACKIE1     0x0000000f
44 #define VEN_MACKIE2     0x00000ff2
45 #define VEN_STANTON     0x00001260
46 #define VEN_TASCAM      0x0000022e
47 #define VEN_BEHRINGER   0x00001564
48 #define VEN_APOGEE      0x000003db
49 #define VEN_ESI         0x00000f1b
50 #define VEN_ACOUSTIC    0x00000002
51 #define VEN_CME         0x0000000a
52 #define VEN_PHONIC      0x00001496
53 #define VEN_LYNX        0x000019e5
54 #define VEN_ICON        0x00001a9e
55 #define VEN_PRISMSOUND  0x00001198
56 #define VEN_TERRATEC    0x00000aac
57 #define VEN_YAMAHA      0x0000a0de
58 #define VEN_FOCUSRITE   0x0000130e
59 #define VEN_MAUDIO1     0x00000d6c
60 #define VEN_MAUDIO2     0x000007f5
61 #define VEN_DIGIDESIGN  0x00a07e
62
63 #define MODEL_FOCUSRITE_SAFFIRE_BOTH    0x00000000
64 #define MODEL_MAUDIO_AUDIOPHILE_BOTH    0x00010060
65 #define MODEL_MAUDIO_FW1814             0x00010071
66 #define MODEL_MAUDIO_PROJECTMIX         0x00010091
67
68 static int
69 name_device(struct snd_bebob *bebob)
70 {
71         struct fw_device *fw_dev = fw_parent_device(bebob->unit);
72         char vendor[24] = {0};
73         char model[32] = {0};
74         u32 hw_id;
75         u32 data[2] = {0};
76         u32 revision;
77         u32 version;
78         int err;
79
80         /* get vendor name from root directory */
81         err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
82                             vendor, sizeof(vendor));
83         if (err < 0)
84                 goto end;
85
86         /* get model name from unit directory */
87         err = fw_csr_string(bebob->unit->directory, CSR_MODEL,
88                             model, sizeof(model));
89         if (err < 0)
90                 goto end;
91
92         /* get hardware id */
93         err = snd_bebob_read_quad(bebob->unit, INFO_OFFSET_HW_MODEL_ID,
94                                   &hw_id);
95         if (err < 0)
96                 goto end;
97
98         /* get hardware revision */
99         err = snd_bebob_read_quad(bebob->unit, INFO_OFFSET_HW_MODEL_REVISION,
100                                   &revision);
101         if (err < 0)
102                 goto end;
103
104         /* get GUID */
105         err = snd_bebob_read_block(bebob->unit, INFO_OFFSET_GUID,
106                                    data, sizeof(data));
107         if (err < 0)
108                 goto end;
109
110         err = snd_bebob_read_quad(bebob->unit, INFO_OFFSET_BEBOB_VERSION,
111                                   &version);
112         if (err < 0)
113                 goto end;
114         bebob->version = version;
115
116         strcpy(bebob->card->driver, "BeBoB");
117         strcpy(bebob->card->shortname, model);
118         strcpy(bebob->card->mixername, model);
119         snprintf(bebob->card->longname, sizeof(bebob->card->longname),
120                  "%s %s (id:%d, rev:%d), GUID %08x%08x at %s, S%d",
121                  vendor, model, hw_id, revision,
122                  data[0], data[1], dev_name(&bebob->unit->device),
123                  100 << fw_dev->max_speed);
124 end:
125         return err;
126 }
127
128 static void
129 bebob_card_free(struct snd_card *card)
130 {
131         struct snd_bebob *bebob = card->private_data;
132
133         mutex_lock(&devices_mutex);
134         clear_bit(bebob->card_index, devices_used);
135         mutex_unlock(&devices_mutex);
136
137         snd_bebob_stream_destroy_duplex(bebob);
138 }
139
140 static const struct snd_bebob_spec *
141 get_saffire_spec(struct fw_unit *unit)
142 {
143         char name[24] = {0};
144
145         if (fw_csr_string(unit->directory, CSR_MODEL, name, sizeof(name)) < 0)
146                 return NULL;
147
148         if (strcmp(name, "SaffireLE") == 0)
149                 return &saffire_le_spec;
150         else
151                 return &saffire_spec;
152 }
153
154 static bool
155 check_audiophile_booted(struct fw_unit *unit)
156 {
157         char name[28] = {0};
158
159         if (fw_csr_string(unit->directory, CSR_MODEL, name, sizeof(name)) < 0)
160                 return false;
161
162         return strncmp(name, "FW Audiophile Bootloader", 24) != 0;
163 }
164
165 static void
166 do_registration(struct work_struct *work)
167 {
168         struct snd_bebob *bebob =
169                         container_of(work, struct snd_bebob, dwork.work);
170         unsigned int card_index;
171         int err;
172
173         if (bebob->registered)
174                 return;
175
176         mutex_lock(&devices_mutex);
177         for (card_index = 0; card_index < SNDRV_CARDS; card_index++) {
178                 if (!test_bit(card_index, devices_used) && enable[card_index])
179                         break;
180         }
181         if (card_index >= SNDRV_CARDS) {
182                 mutex_unlock(&devices_mutex);
183                 return;
184         }
185
186         err = snd_card_new(&bebob->unit->device, index[card_index],
187                            id[card_index], THIS_MODULE, 0, &bebob->card);
188         if (err < 0) {
189                 mutex_unlock(&devices_mutex);
190                 return;
191         }
192         set_bit(card_index, devices_used);
193         mutex_unlock(&devices_mutex);
194
195         bebob->card->private_free = bebob_card_free;
196         bebob->card->private_data = bebob;
197
198         err = name_device(bebob);
199         if (err < 0)
200                 goto error;
201
202         if (bebob->spec == &maudio_special_spec) {
203                 if (bebob->entry->model_id == MODEL_MAUDIO_FW1814)
204                         err = snd_bebob_maudio_special_discover(bebob, true);
205                 else
206                         err = snd_bebob_maudio_special_discover(bebob, false);
207         } else {
208                 err = snd_bebob_stream_discover(bebob);
209         }
210         if (err < 0)
211                 goto error;
212
213         err = snd_bebob_stream_init_duplex(bebob);
214         if (err < 0)
215                 goto error;
216
217         snd_bebob_proc_init(bebob);
218
219         if (bebob->midi_input_ports > 0 || bebob->midi_output_ports > 0) {
220                 err = snd_bebob_create_midi_devices(bebob);
221                 if (err < 0)
222                         goto error;
223         }
224
225         err = snd_bebob_create_pcm_devices(bebob);
226         if (err < 0)
227                 goto error;
228
229         err = snd_bebob_create_hwdep_device(bebob);
230         if (err < 0)
231                 goto error;
232
233         err = snd_card_register(bebob->card);
234         if (err < 0)
235                 goto error;
236
237         bebob->registered = true;
238
239         return;
240 error:
241         snd_card_free(bebob->card);
242         dev_info(&bebob->unit->device,
243                  "Sound card registration failed: %d\n", err);
244 }
245
246 static int
247 bebob_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
248 {
249         struct snd_bebob *bebob;
250         const struct snd_bebob_spec *spec;
251
252         if (entry->vendor_id == VEN_FOCUSRITE &&
253             entry->model_id == MODEL_FOCUSRITE_SAFFIRE_BOTH)
254                 spec = get_saffire_spec(unit);
255         else if (entry->vendor_id == VEN_MAUDIO1 &&
256                  entry->model_id == MODEL_MAUDIO_AUDIOPHILE_BOTH &&
257                  !check_audiophile_booted(unit))
258                 spec = NULL;
259         else
260                 spec = (const struct snd_bebob_spec *)entry->driver_data;
261
262         if (spec == NULL) {
263                 if (entry->vendor_id == VEN_MAUDIO1 ||
264                     entry->vendor_id == VEN_MAUDIO2)
265                         return snd_bebob_maudio_load_firmware(unit);
266                 else
267                         return -ENODEV;
268         }
269
270         /* Allocate this independent of sound card instance. */
271         bebob = devm_kzalloc(&unit->device, sizeof(struct snd_bebob),
272                              GFP_KERNEL);
273         if (!bebob)
274                 return -ENOMEM;
275         bebob->unit = fw_unit_get(unit);
276         dev_set_drvdata(&unit->device, bebob);
277
278         bebob->entry = entry;
279         bebob->spec = spec;
280         mutex_init(&bebob->mutex);
281         spin_lock_init(&bebob->lock);
282         init_waitqueue_head(&bebob->hwdep_wait);
283
284         /* Allocate and register this sound card later. */
285         INIT_DEFERRABLE_WORK(&bebob->dwork, do_registration);
286
287         if (entry->vendor_id != VEN_MAUDIO1 ||
288             (entry->model_id != MODEL_MAUDIO_FW1814 &&
289              entry->model_id != MODEL_MAUDIO_PROJECTMIX)) {
290                 snd_fw_schedule_registration(unit, &bebob->dwork);
291         } else {
292                 /*
293                  * This is a workaround. This bus reset seems to have an effect
294                  * to make devices correctly handling transactions. Without
295                  * this, the devices have gap_count mismatch. This causes much
296                  * failure of transaction.
297                  *
298                  * Just after registration, user-land application receive
299                  * signals from dbus and starts I/Os. To avoid I/Os till the
300                  * future bus reset, registration is done in next update().
301                  */
302                 fw_schedule_bus_reset(fw_parent_device(bebob->unit)->card,
303                                       false, true);
304         }
305
306         return 0;
307 }
308
309 /*
310  * This driver doesn't update streams in bus reset handler.
311  *
312  * DM1000/ DM1100/DM1500 chipsets with BeBoB firmware transfer packets with
313  * discontinued counter at bus reset. This discontinuity is immediately
314  * detected in packet streaming layer, then it sets XRUN to PCM substream.
315  *
316  * ALSA PCM applications can know the XRUN by getting -EPIPE from PCM operation.
317  * Then, they can recover the PCM substream by executing ioctl(2) with
318  * SNDRV_PCM_IOCTL_PREPARE. 'struct snd_pcm_ops.prepare' is called and drivers
319  * restart packet streaming.
320  *
321  * The above processing may be executed before this bus-reset handler is
322  * executed. When this handler updates streams with current isochronous
323  * channels, the streams already have the current ones.
324  */
325 static void
326 bebob_update(struct fw_unit *unit)
327 {
328         struct snd_bebob *bebob = dev_get_drvdata(&unit->device);
329
330         if (bebob == NULL)
331                 return;
332
333         /* Postpone a workqueue for deferred registration. */
334         if (!bebob->registered)
335                 snd_fw_schedule_registration(unit, &bebob->dwork);
336         else
337                 fcp_bus_reset(bebob->unit);
338 }
339
340 static void bebob_remove(struct fw_unit *unit)
341 {
342         struct snd_bebob *bebob = dev_get_drvdata(&unit->device);
343
344         if (bebob == NULL)
345                 return;
346
347         /*
348          * Confirm to stop the work for registration before the sound card is
349          * going to be released. The work is not scheduled again because bus
350          * reset handler is not called anymore.
351          */
352         cancel_delayed_work_sync(&bebob->dwork);
353
354         if (bebob->registered) {
355                 // Block till all of ALSA character devices are released.
356                 snd_card_free(bebob->card);
357         }
358
359         mutex_destroy(&bebob->mutex);
360         fw_unit_put(bebob->unit);
361 }
362
363 static const struct snd_bebob_rate_spec normal_rate_spec = {
364         .get    = &snd_bebob_stream_get_rate,
365         .set    = &snd_bebob_stream_set_rate
366 };
367 static const struct snd_bebob_spec spec_normal = {
368         .clock  = NULL,
369         .rate   = &normal_rate_spec,
370         .meter  = NULL
371 };
372
373 static const struct ieee1394_device_id bebob_id_table[] = {
374         /* Edirol, FA-66 */
375         SND_BEBOB_DEV_ENTRY(VEN_EDIROL, 0x00010049, &spec_normal),
376         /* Edirol, FA-101 */
377         SND_BEBOB_DEV_ENTRY(VEN_EDIROL, 0x00010048, &spec_normal),
378         /* Presonus, FIREBOX */
379         SND_BEBOB_DEV_ENTRY(VEN_PRESONUS, 0x00010000, &spec_normal),
380         /* PreSonus, FIREPOD/FP10 */
381         SND_BEBOB_DEV_ENTRY(VEN_PRESONUS, 0x00010066, &spec_normal),
382         /* PreSonus, Inspire1394 */
383         SND_BEBOB_DEV_ENTRY(VEN_PRESONUS, 0x00010001, &spec_normal),
384         /* BridgeCo, RDAudio1 */
385         SND_BEBOB_DEV_ENTRY(VEN_BRIDGECO, 0x00010048, &spec_normal),
386         /* BridgeCo, Audio5 */
387         SND_BEBOB_DEV_ENTRY(VEN_BRIDGECO, 0x00010049, &spec_normal),
388         /* Mackie, Onyx 1220/1620/1640 (Firewire I/O Card) */
389         SND_BEBOB_DEV_ENTRY(VEN_MACKIE2, 0x00010065, &spec_normal),
390         /* Mackie, d.2 (Firewire Option) */
391         SND_BEBOB_DEV_ENTRY(VEN_MACKIE1, 0x00010067, &spec_normal),
392         /* Stanton, ScratchAmp */
393         SND_BEBOB_DEV_ENTRY(VEN_STANTON, 0x00000001, &spec_normal),
394         /* Tascam, IF-FW DM */
395         SND_BEBOB_DEV_ENTRY(VEN_TASCAM, 0x00010067, &spec_normal),
396         /* Behringer, XENIX UFX 1204 */
397         SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x00001204, &spec_normal),
398         /* Behringer, XENIX UFX 1604 */
399         SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x00001604, &spec_normal),
400         /* Behringer, Digital Mixer X32 series (X-UF Card) */
401         SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x00000006, &spec_normal),
402         /*  Behringer, F-Control Audio 1616 */
403         SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x001616, &spec_normal),
404         /*  Behringer, F-Control Audio 610 */
405         SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x000610, &spec_normal),
406         /* Apogee Electronics, Rosetta 200/400 (X-FireWire card) */
407         /* Apogee Electronics, DA/AD/DD-16X (X-FireWire card) */
408         SND_BEBOB_DEV_ENTRY(VEN_APOGEE, 0x00010048, &spec_normal),
409         /* Apogee Electronics, Ensemble */
410         SND_BEBOB_DEV_ENTRY(VEN_APOGEE, 0x01eeee, &spec_normal),
411         /* ESI, Quatafire610 */
412         SND_BEBOB_DEV_ENTRY(VEN_ESI, 0x00010064, &spec_normal),
413         /* AcousticReality, eARMasterOne */
414         SND_BEBOB_DEV_ENTRY(VEN_ACOUSTIC, 0x00000002, &spec_normal),
415         /* CME, MatrixKFW */
416         SND_BEBOB_DEV_ENTRY(VEN_CME, 0x00030000, &spec_normal),
417         /* Phonic, Helix Board 12 MkII */
418         SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00050000, &spec_normal),
419         /* Phonic, Helix Board 18 MkII */
420         SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00060000, &spec_normal),
421         /* Phonic, Helix Board 24 MkII */
422         SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00070000, &spec_normal),
423         /* Phonic, Helix Board 12 Universal/18 Universal/24 Universal */
424         SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00000000, &spec_normal),
425         /* Lynx, Aurora 8/16 (LT-FW) */
426         SND_BEBOB_DEV_ENTRY(VEN_LYNX, 0x00000001, &spec_normal),
427         /* ICON, FireXon */
428         SND_BEBOB_DEV_ENTRY(VEN_ICON, 0x00000001, &spec_normal),
429         /* PrismSound, Orpheus */
430         SND_BEBOB_DEV_ENTRY(VEN_PRISMSOUND, 0x00010048, &spec_normal),
431         /* PrismSound, ADA-8XR */
432         SND_BEBOB_DEV_ENTRY(VEN_PRISMSOUND, 0x0000ada8, &spec_normal),
433         /* TerraTec Electronic GmbH, PHASE 88 Rack FW */
434         SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000003, &phase88_rack_spec),
435         /* TerraTec Electronic GmbH, PHASE 24 FW */
436         SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000004, &yamaha_terratec_spec),
437         /* TerraTec Electronic GmbH, Phase X24 FW */
438         SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000007, &yamaha_terratec_spec),
439         /* TerraTec Electronic GmbH, EWS MIC2/MIC8 */
440         SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000005, &spec_normal),
441         /* Terratec Electronic GmbH, Aureon 7.1 Firewire */
442         SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000002, &spec_normal),
443         /* Yamaha, GO44 */
444         SND_BEBOB_DEV_ENTRY(VEN_YAMAHA, 0x0010000b, &yamaha_terratec_spec),
445         /* YAMAHA, GO46 */
446         SND_BEBOB_DEV_ENTRY(VEN_YAMAHA, 0x0010000c, &yamaha_terratec_spec),
447         /* Focusrite, SaffirePro 26 I/O */
448         SND_BEBOB_DEV_ENTRY(VEN_FOCUSRITE, 0x00000003, &saffirepro_26_spec),
449         /* Focusrite, SaffirePro 10 I/O */
450         {
451                 // The combination of vendor_id and model_id is the same as the
452                 // same as the one of Liquid Saffire 56.
453                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
454                                   IEEE1394_MATCH_MODEL_ID |
455                                   IEEE1394_MATCH_SPECIFIER_ID |
456                                   IEEE1394_MATCH_VERSION,
457                 .vendor_id      = VEN_FOCUSRITE,
458                 .model_id       = 0x000006,
459                 .specifier_id   = 0x00a02d,
460                 .version        = 0x010001,
461                 .driver_data    = (kernel_ulong_t)&saffirepro_10_spec,
462         },
463         /* Focusrite, Saffire(no label and LE) */
464         SND_BEBOB_DEV_ENTRY(VEN_FOCUSRITE, MODEL_FOCUSRITE_SAFFIRE_BOTH,
465                             &saffire_spec),
466         /* M-Audio, Firewire 410 */
467         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO2, 0x00010058, NULL),     /* bootloader */
468         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO2, 0x00010046, &maudio_fw410_spec),
469         /* M-Audio, Firewire Audiophile */
470         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, MODEL_MAUDIO_AUDIOPHILE_BOTH,
471                             &maudio_audiophile_spec),
472         /* M-Audio, Firewire Solo */
473         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x00010062, &maudio_solo_spec),
474         /* M-Audio, Ozonic */
475         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x0000000a, &maudio_ozonic_spec),
476         /* M-Audio NRV10 */
477         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x00010081, &maudio_nrv10_spec),
478         /* M-Audio, ProFireLightbridge */
479         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x000100a1, &spec_normal),
480         /* Firewire 1814 */
481         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x00010070, NULL),     /* bootloader */
482         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, MODEL_MAUDIO_FW1814,
483                             &maudio_special_spec),
484         /* M-Audio ProjectMix */
485         SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, MODEL_MAUDIO_PROJECTMIX,
486                             &maudio_special_spec),
487         /* Digidesign Mbox 2 Pro */
488         SND_BEBOB_DEV_ENTRY(VEN_DIGIDESIGN, 0x0000a9, &spec_normal),
489         /* IDs are unknown but able to be supported */
490         /*  Apogee, Mini-ME Firewire */
491         /*  Apogee, Mini-DAC Firewire */
492         /*  Cakawalk, Sonar Power Studio 66 */
493         /*  CME, UF400e */
494         /*  ESI, Quotafire XL */
495         /*  Infrasonic, DewX */
496         /*  Infrasonic, Windy6 */
497         /*  Mackie, Digital X Bus x.200 */
498         /*  Mackie, Digital X Bus x.400 */
499         /*  Phonic, HB 12 */
500         /*  Phonic, HB 24 */
501         /*  Phonic, HB 18 */
502         /*  Phonic, FireFly 202 */
503         /*  Phonic, FireFly 302 */
504         /*  Rolf Spuler, Firewire Guitar */
505         {}
506 };
507 MODULE_DEVICE_TABLE(ieee1394, bebob_id_table);
508
509 static struct fw_driver bebob_driver = {
510         .driver = {
511                 .owner  = THIS_MODULE,
512                 .name   = "snd-bebob",
513                 .bus    = &fw_bus_type,
514         },
515         .probe    = bebob_probe,
516         .update   = bebob_update,
517         .remove   = bebob_remove,
518         .id_table = bebob_id_table,
519 };
520
521 static int __init
522 snd_bebob_init(void)
523 {
524         return driver_register(&bebob_driver.driver);
525 }
526
527 static void __exit
528 snd_bebob_exit(void)
529 {
530         driver_unregister(&bebob_driver.driver);
531 }
532
533 module_init(snd_bebob_init);
534 module_exit(snd_bebob_exit);