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