dice.c 8.6 KB

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