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