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