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