dice.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 OUI_FOCUSRITE 0x00130e
  14. #define OUI_TCELECTRONIC 0x000166
  15. #define DICE_CATEGORY_ID 0x04
  16. #define WEISS_CATEGORY_ID 0x00
  17. #define LOUD_CATEGORY_ID 0x10
  18. /*
  19. * Some models support several isochronous channels, while these streams are not
  20. * always available. In this case, add the model name to this list.
  21. */
  22. static bool force_two_pcm_support(struct fw_unit *unit)
  23. {
  24. const char *const models[] = {
  25. /* TC Electronic models. */
  26. "StudioKonnekt48",
  27. /* Focusrite models. */
  28. "SAFFIRE_PRO_40",
  29. "LIQUID_SAFFIRE_56",
  30. "SAFFIRE_PRO_40_1",
  31. };
  32. char model[32];
  33. unsigned int i;
  34. int err;
  35. err = fw_csr_string(unit->directory, CSR_MODEL, model, sizeof(model));
  36. if (err < 0)
  37. return false;
  38. for (i = 0; i < ARRAY_SIZE(models); i++) {
  39. if (strcmp(models[i], model) == 0)
  40. break;
  41. }
  42. return i < ARRAY_SIZE(models);
  43. }
  44. static int check_dice_category(struct fw_unit *unit)
  45. {
  46. struct fw_device *device = fw_parent_device(unit);
  47. struct fw_csr_iterator it;
  48. int key, val, vendor = -1, model = -1;
  49. unsigned int category;
  50. /*
  51. * Check that GUID and unit directory are constructed according to DICE
  52. * rules, i.e., that the specifier ID is the GUID's OUI, and that the
  53. * GUID chip ID consists of the 8-bit category ID, the 10-bit product
  54. * ID, and a 22-bit serial number.
  55. */
  56. fw_csr_iterator_init(&it, unit->directory);
  57. while (fw_csr_iterator_next(&it, &key, &val)) {
  58. switch (key) {
  59. case CSR_SPECIFIER_ID:
  60. vendor = val;
  61. break;
  62. case CSR_MODEL:
  63. model = val;
  64. break;
  65. }
  66. }
  67. if (vendor == OUI_FOCUSRITE || vendor == OUI_TCELECTRONIC) {
  68. if (force_two_pcm_support(unit))
  69. return 0;
  70. }
  71. if (vendor == OUI_WEISS)
  72. category = WEISS_CATEGORY_ID;
  73. else if (vendor == OUI_LOUD)
  74. category = LOUD_CATEGORY_ID;
  75. else
  76. category = DICE_CATEGORY_ID;
  77. if (device->config_rom[3] != ((vendor << 8) | category) ||
  78. device->config_rom[4] >> 22 != model)
  79. return -ENODEV;
  80. return 0;
  81. }
  82. static int check_clock_caps(struct snd_dice *dice)
  83. {
  84. __be32 value;
  85. int err;
  86. /* some very old firmwares don't tell about their clock support */
  87. if (dice->clock_caps > 0) {
  88. err = snd_dice_transaction_read_global(dice,
  89. GLOBAL_CLOCK_CAPABILITIES,
  90. &value, 4);
  91. if (err < 0)
  92. return err;
  93. dice->clock_caps = be32_to_cpu(value);
  94. } else {
  95. /* this should be supported by any device */
  96. dice->clock_caps = CLOCK_CAP_RATE_44100 |
  97. CLOCK_CAP_RATE_48000 |
  98. CLOCK_CAP_SOURCE_ARX1 |
  99. CLOCK_CAP_SOURCE_INTERNAL;
  100. }
  101. return 0;
  102. }
  103. static void dice_card_strings(struct snd_dice *dice)
  104. {
  105. struct snd_card *card = dice->card;
  106. struct fw_device *dev = fw_parent_device(dice->unit);
  107. char vendor[32], model[32];
  108. unsigned int i;
  109. int err;
  110. strcpy(card->driver, "DICE");
  111. strcpy(card->shortname, "DICE");
  112. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  113. err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
  114. card->shortname,
  115. sizeof(card->shortname));
  116. if (err >= 0) {
  117. /* DICE strings are returned in "always-wrong" endianness */
  118. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  119. for (i = 0; i < sizeof(card->shortname); i += 4)
  120. swab32s((u32 *)&card->shortname[i]);
  121. card->shortname[sizeof(card->shortname) - 1] = '\0';
  122. }
  123. strcpy(vendor, "?");
  124. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  125. strcpy(model, "?");
  126. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  127. snprintf(card->longname, sizeof(card->longname),
  128. "%s %s (serial %u) at %s, S%d",
  129. vendor, model, dev->config_rom[4] & 0x3fffff,
  130. dev_name(&dice->unit->device), 100 << dev->max_speed);
  131. strcpy(card->mixername, "DICE");
  132. }
  133. static void dice_free(struct snd_dice *dice)
  134. {
  135. snd_dice_stream_destroy_duplex(dice);
  136. snd_dice_transaction_destroy(dice);
  137. fw_unit_put(dice->unit);
  138. mutex_destroy(&dice->mutex);
  139. kfree(dice);
  140. }
  141. /*
  142. * This module releases the FireWire unit data after all ALSA character devices
  143. * are released by applications. This is for releasing stream data or finishing
  144. * transactions safely. Thus at returning from .remove(), this module still keep
  145. * references for the unit.
  146. */
  147. static void dice_card_free(struct snd_card *card)
  148. {
  149. dice_free(card->private_data);
  150. }
  151. static void do_registration(struct work_struct *work)
  152. {
  153. struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
  154. int err;
  155. if (dice->registered)
  156. return;
  157. err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
  158. &dice->card);
  159. if (err < 0)
  160. return;
  161. if (force_two_pcm_support(dice->unit))
  162. dice->force_two_pcms = true;
  163. err = snd_dice_transaction_init(dice);
  164. if (err < 0)
  165. goto error;
  166. err = check_clock_caps(dice);
  167. if (err < 0)
  168. goto error;
  169. dice_card_strings(dice);
  170. err = snd_dice_stream_init_duplex(dice);
  171. if (err < 0)
  172. goto error;
  173. snd_dice_create_proc(dice);
  174. err = snd_dice_create_pcm(dice);
  175. if (err < 0)
  176. goto error;
  177. err = snd_dice_create_midi(dice);
  178. if (err < 0)
  179. goto error;
  180. err = snd_dice_create_hwdep(dice);
  181. if (err < 0)
  182. goto error;
  183. err = snd_card_register(dice->card);
  184. if (err < 0)
  185. goto error;
  186. /*
  187. * After registered, dice instance can be released corresponding to
  188. * releasing the sound card instance.
  189. */
  190. dice->card->private_free = dice_card_free;
  191. dice->card->private_data = dice;
  192. dice->registered = true;
  193. return;
  194. error:
  195. snd_dice_stream_destroy_duplex(dice);
  196. snd_dice_transaction_destroy(dice);
  197. snd_dice_stream_destroy_duplex(dice);
  198. snd_card_free(dice->card);
  199. dev_info(&dice->unit->device,
  200. "Sound card registration failed: %d\n", err);
  201. }
  202. static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  203. {
  204. struct snd_dice *dice;
  205. int err;
  206. err = check_dice_category(unit);
  207. if (err < 0)
  208. return -ENODEV;
  209. /* Allocate this independent of sound card instance. */
  210. dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
  211. if (dice == NULL)
  212. return -ENOMEM;
  213. dice->unit = fw_unit_get(unit);
  214. dev_set_drvdata(&unit->device, dice);
  215. spin_lock_init(&dice->lock);
  216. mutex_init(&dice->mutex);
  217. init_completion(&dice->clock_accepted);
  218. init_waitqueue_head(&dice->hwdep_wait);
  219. /* Allocate and register this sound card later. */
  220. INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
  221. snd_fw_schedule_registration(unit, &dice->dwork);
  222. return 0;
  223. }
  224. static void dice_remove(struct fw_unit *unit)
  225. {
  226. struct snd_dice *dice = dev_get_drvdata(&unit->device);
  227. /*
  228. * Confirm to stop the work for registration before the sound card is
  229. * going to be released. The work is not scheduled again because bus
  230. * reset handler is not called anymore.
  231. */
  232. cancel_delayed_work_sync(&dice->dwork);
  233. if (dice->registered) {
  234. /* No need to wait for releasing card object in this context. */
  235. snd_card_free_when_closed(dice->card);
  236. } else {
  237. /* Don't forget this case. */
  238. dice_free(dice);
  239. }
  240. }
  241. static void dice_bus_reset(struct fw_unit *unit)
  242. {
  243. struct snd_dice *dice = dev_get_drvdata(&unit->device);
  244. /* Postpone a workqueue for deferred registration. */
  245. if (!dice->registered)
  246. snd_fw_schedule_registration(unit, &dice->dwork);
  247. /* The handler address register becomes initialized. */
  248. snd_dice_transaction_reinit(dice);
  249. /*
  250. * After registration, userspace can start packet streaming, then this
  251. * code block works fine.
  252. */
  253. if (dice->registered) {
  254. mutex_lock(&dice->mutex);
  255. snd_dice_stream_update_duplex(dice);
  256. mutex_unlock(&dice->mutex);
  257. }
  258. }
  259. #define DICE_INTERFACE 0x000001
  260. static const struct ieee1394_device_id dice_id_table[] = {
  261. {
  262. .match_flags = IEEE1394_MATCH_VERSION,
  263. .version = DICE_INTERFACE,
  264. },
  265. /* M-Audio Profire 610/2626 has a different value in version field. */
  266. {
  267. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  268. IEEE1394_MATCH_SPECIFIER_ID,
  269. .vendor_id = 0x000d6c,
  270. .specifier_id = 0x000d6c,
  271. },
  272. { }
  273. };
  274. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  275. static struct fw_driver dice_driver = {
  276. .driver = {
  277. .owner = THIS_MODULE,
  278. .name = KBUILD_MODNAME,
  279. .bus = &fw_bus_type,
  280. },
  281. .probe = dice_probe,
  282. .update = dice_bus_reset,
  283. .remove = dice_remove,
  284. .id_table = dice_id_table,
  285. };
  286. static int __init alsa_dice_init(void)
  287. {
  288. return driver_register(&dice_driver.driver);
  289. }
  290. static void __exit alsa_dice_exit(void)
  291. {
  292. driver_unregister(&dice_driver.driver);
  293. }
  294. module_init(alsa_dice_init);
  295. module_exit(alsa_dice_exit);