dice.c 8.7 KB

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