1 /* 2 * TC Applied Technologies Digital Interface Communications Engine driver 3 * 4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 5 * Licensed under the terms of the GNU General Public License, version 2. 6 */ 7 8 #include "dice.h" 9 10 MODULE_DESCRIPTION("DICE driver"); 11 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 12 MODULE_LICENSE("GPL v2"); 13 14 #define OUI_WEISS 0x001c6a 15 16 #define DICE_CATEGORY_ID 0x04 17 #define WEISS_CATEGORY_ID 0x00 18 19 static int dice_interface_check(struct fw_unit *unit) 20 { 21 static const int min_values[10] = { 22 10, 0x64 / 4, 23 10, 0x18 / 4, 24 10, 0x18 / 4, 25 0, 0, 26 0, 0, 27 }; 28 struct fw_device *device = fw_parent_device(unit); 29 struct fw_csr_iterator it; 30 int key, val, vendor = -1, model = -1, err; 31 unsigned int category, i; 32 __be32 *pointers; 33 u32 value; 34 __be32 version; 35 36 pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32), 37 GFP_KERNEL); 38 if (pointers == NULL) 39 return -ENOMEM; 40 41 /* 42 * Check that GUID and unit directory are constructed according to DICE 43 * rules, i.e., that the specifier ID is the GUID's OUI, and that the 44 * GUID chip ID consists of the 8-bit category ID, the 10-bit product 45 * ID, and a 22-bit serial number. 46 */ 47 fw_csr_iterator_init(&it, unit->directory); 48 while (fw_csr_iterator_next(&it, &key, &val)) { 49 switch (key) { 50 case CSR_SPECIFIER_ID: 51 vendor = val; 52 break; 53 case CSR_MODEL: 54 model = val; 55 break; 56 } 57 } 58 if (vendor == OUI_WEISS) 59 category = WEISS_CATEGORY_ID; 60 else 61 category = DICE_CATEGORY_ID; 62 if (device->config_rom[3] != ((vendor << 8) | category) || 63 device->config_rom[4] >> 22 != model) { 64 err = -ENODEV; 65 goto end; 66 } 67 68 /* 69 * Check that the sub address spaces exist and are located inside the 70 * private address space. The minimum values are chosen so that all 71 * minimally required registers are included. 72 */ 73 err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST, 74 DICE_PRIVATE_SPACE, pointers, 75 sizeof(__be32) * ARRAY_SIZE(min_values), 0); 76 if (err < 0) { 77 err = -ENODEV; 78 goto end; 79 } 80 for (i = 0; i < ARRAY_SIZE(min_values); ++i) { 81 value = be32_to_cpu(pointers[i]); 82 if (value < min_values[i] || value >= 0x40000) { 83 err = -ENODEV; 84 goto end; 85 } 86 } 87 88 /* 89 * Check that the implemented DICE driver specification major version 90 * number matches. 91 */ 92 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST, 93 DICE_PRIVATE_SPACE + 94 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION, 95 &version, 4, 0); 96 if (err < 0) { 97 err = -ENODEV; 98 goto end; 99 } 100 if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) { 101 dev_err(&unit->device, 102 "unknown DICE version: 0x%08x\n", be32_to_cpu(version)); 103 err = -ENODEV; 104 goto end; 105 } 106 end: 107 return err; 108 } 109 110 static int highest_supported_mode_rate(struct snd_dice *dice, 111 unsigned int mode, unsigned int *rate) 112 { 113 unsigned int i, m; 114 115 for (i = ARRAY_SIZE(snd_dice_rates); i > 0; i--) { 116 *rate = snd_dice_rates[i - 1]; 117 if (snd_dice_stream_get_rate_mode(dice, *rate, &m) < 0) 118 continue; 119 if (mode == m) 120 break; 121 } 122 if (i == 0) 123 return -EINVAL; 124 125 return 0; 126 } 127 128 static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode) 129 { 130 __be32 values[2]; 131 unsigned int rate; 132 int err; 133 134 if (highest_supported_mode_rate(dice, mode, &rate) < 0) { 135 dice->tx_channels[mode] = 0; 136 dice->tx_midi_ports[mode] = 0; 137 dice->rx_channels[mode] = 0; 138 dice->rx_midi_ports[mode] = 0; 139 return 0; 140 } 141 142 err = snd_dice_transaction_set_rate(dice, rate); 143 if (err < 0) 144 return err; 145 146 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO, 147 values, sizeof(values)); 148 if (err < 0) 149 return err; 150 151 dice->tx_channels[mode] = be32_to_cpu(values[0]); 152 dice->tx_midi_ports[mode] = be32_to_cpu(values[1]); 153 154 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO, 155 values, sizeof(values)); 156 if (err < 0) 157 return err; 158 159 dice->rx_channels[mode] = be32_to_cpu(values[0]); 160 dice->rx_midi_ports[mode] = be32_to_cpu(values[1]); 161 162 return 0; 163 } 164 165 static int dice_read_params(struct snd_dice *dice) 166 { 167 __be32 value; 168 int mode, err; 169 170 /* some very old firmwares don't tell about their clock support */ 171 if (dice->clock_caps > 0) { 172 err = snd_dice_transaction_read_global(dice, 173 GLOBAL_CLOCK_CAPABILITIES, 174 &value, 4); 175 if (err < 0) 176 return err; 177 dice->clock_caps = be32_to_cpu(value); 178 } else { 179 /* this should be supported by any device */ 180 dice->clock_caps = CLOCK_CAP_RATE_44100 | 181 CLOCK_CAP_RATE_48000 | 182 CLOCK_CAP_SOURCE_ARX1 | 183 CLOCK_CAP_SOURCE_INTERNAL; 184 } 185 186 for (mode = 2; mode >= 0; --mode) { 187 err = dice_read_mode_params(dice, mode); 188 if (err < 0) 189 return err; 190 } 191 192 return 0; 193 } 194 195 static void dice_card_strings(struct snd_dice *dice) 196 { 197 struct snd_card *card = dice->card; 198 struct fw_device *dev = fw_parent_device(dice->unit); 199 char vendor[32], model[32]; 200 unsigned int i; 201 int err; 202 203 strcpy(card->driver, "DICE"); 204 205 strcpy(card->shortname, "DICE"); 206 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname)); 207 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME, 208 card->shortname, 209 sizeof(card->shortname)); 210 if (err >= 0) { 211 /* DICE strings are returned in "always-wrong" endianness */ 212 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0); 213 for (i = 0; i < sizeof(card->shortname); i += 4) 214 swab32s((u32 *)&card->shortname[i]); 215 card->shortname[sizeof(card->shortname) - 1] = '\0'; 216 } 217 218 strcpy(vendor, "?"); 219 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor)); 220 strcpy(model, "?"); 221 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model)); 222 snprintf(card->longname, sizeof(card->longname), 223 "%s %s (serial %u) at %s, S%d", 224 vendor, model, dev->config_rom[4] & 0x3fffff, 225 dev_name(&dice->unit->device), 100 << dev->max_speed); 226 227 strcpy(card->mixername, "DICE"); 228 } 229 230 /* 231 * This module releases the FireWire unit data after all ALSA character devices 232 * are released by applications. This is for releasing stream data or finishing 233 * transactions safely. Thus at returning from .remove(), this module still keep 234 * references for the unit. 235 */ 236 static void dice_card_free(struct snd_card *card) 237 { 238 struct snd_dice *dice = card->private_data; 239 240 snd_dice_stream_destroy_duplex(dice); 241 snd_dice_transaction_destroy(dice); 242 fw_unit_put(dice->unit); 243 244 mutex_destroy(&dice->mutex); 245 } 246 247 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id) 248 { 249 struct snd_card *card; 250 struct snd_dice *dice; 251 int err; 252 253 err = dice_interface_check(unit); 254 if (err < 0) 255 goto end; 256 257 err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, 258 sizeof(*dice), &card); 259 if (err < 0) 260 goto end; 261 262 dice = card->private_data; 263 dice->card = card; 264 dice->unit = fw_unit_get(unit); 265 card->private_free = dice_card_free; 266 267 spin_lock_init(&dice->lock); 268 mutex_init(&dice->mutex); 269 init_completion(&dice->clock_accepted); 270 init_waitqueue_head(&dice->hwdep_wait); 271 272 err = snd_dice_transaction_init(dice); 273 if (err < 0) 274 goto error; 275 276 err = dice_read_params(dice); 277 if (err < 0) 278 goto error; 279 280 dice_card_strings(dice); 281 282 err = snd_dice_create_pcm(dice); 283 if (err < 0) 284 goto error; 285 286 err = snd_dice_create_hwdep(dice); 287 if (err < 0) 288 goto error; 289 290 snd_dice_create_proc(dice); 291 292 err = snd_dice_create_midi(dice); 293 if (err < 0) 294 goto error; 295 296 err = snd_dice_stream_init_duplex(dice); 297 if (err < 0) 298 goto error; 299 300 err = snd_card_register(card); 301 if (err < 0) { 302 snd_dice_stream_destroy_duplex(dice); 303 goto error; 304 } 305 306 dev_set_drvdata(&unit->device, dice); 307 end: 308 return err; 309 error: 310 snd_card_free(card); 311 return err; 312 } 313 314 static void dice_remove(struct fw_unit *unit) 315 { 316 struct snd_dice *dice = dev_get_drvdata(&unit->device); 317 318 /* No need to wait for releasing card object in this context. */ 319 snd_card_free_when_closed(dice->card); 320 } 321 322 static void dice_bus_reset(struct fw_unit *unit) 323 { 324 struct snd_dice *dice = dev_get_drvdata(&unit->device); 325 326 /* The handler address register becomes initialized. */ 327 snd_dice_transaction_reinit(dice); 328 329 mutex_lock(&dice->mutex); 330 snd_dice_stream_update_duplex(dice); 331 mutex_unlock(&dice->mutex); 332 } 333 334 #define DICE_INTERFACE 0x000001 335 336 static const struct ieee1394_device_id dice_id_table[] = { 337 { 338 .match_flags = IEEE1394_MATCH_VERSION, 339 .version = DICE_INTERFACE, 340 }, 341 { } 342 }; 343 MODULE_DEVICE_TABLE(ieee1394, dice_id_table); 344 345 static struct fw_driver dice_driver = { 346 .driver = { 347 .owner = THIS_MODULE, 348 .name = KBUILD_MODNAME, 349 .bus = &fw_bus_type, 350 }, 351 .probe = dice_probe, 352 .update = dice_bus_reset, 353 .remove = dice_remove, 354 .id_table = dice_id_table, 355 }; 356 357 static int __init alsa_dice_init(void) 358 { 359 return driver_register(&dice_driver.driver); 360 } 361 362 static void __exit alsa_dice_exit(void) 363 { 364 driver_unregister(&dice_driver.driver); 365 } 366 367 module_init(alsa_dice_init); 368 module_exit(alsa_dice_exit); 369