dice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 OUI_ALESIS 0x000595
  16. #define OUI_MAUDIO 0x000d6c
  17. #define OUI_MYTEK 0x001ee8
  18. #define OUI_SSL 0x0050c2 // Actually ID reserved by IEEE.
  19. #define DICE_CATEGORY_ID 0x04
  20. #define WEISS_CATEGORY_ID 0x00
  21. #define LOUD_CATEGORY_ID 0x10
  22. #define MODEL_ALESIS_IO_BOTH 0x000001
  23. static int check_dice_category(struct fw_unit *unit)
  24. {
  25. struct fw_device *device = fw_parent_device(unit);
  26. struct fw_csr_iterator it;
  27. int key, val, vendor = -1, model = -1;
  28. unsigned int category;
  29. /*
  30. * Check that GUID and unit directory are constructed according to DICE
  31. * rules, i.e., that the specifier ID is the GUID's OUI, and that the
  32. * GUID chip ID consists of the 8-bit category ID, the 10-bit product
  33. * ID, and a 22-bit serial number.
  34. */
  35. fw_csr_iterator_init(&it, unit->directory);
  36. while (fw_csr_iterator_next(&it, &key, &val)) {
  37. switch (key) {
  38. case CSR_SPECIFIER_ID:
  39. vendor = val;
  40. break;
  41. case CSR_MODEL:
  42. model = val;
  43. break;
  44. }
  45. }
  46. if (vendor == OUI_WEISS)
  47. category = WEISS_CATEGORY_ID;
  48. else if (vendor == OUI_LOUD)
  49. category = LOUD_CATEGORY_ID;
  50. else
  51. category = DICE_CATEGORY_ID;
  52. if (device->config_rom[3] != ((vendor << 8) | category) ||
  53. device->config_rom[4] >> 22 != model)
  54. return -ENODEV;
  55. return 0;
  56. }
  57. static int check_clock_caps(struct snd_dice *dice)
  58. {
  59. __be32 value;
  60. int err;
  61. /* some very old firmwares don't tell about their clock support */
  62. if (dice->clock_caps > 0) {
  63. err = snd_dice_transaction_read_global(dice,
  64. GLOBAL_CLOCK_CAPABILITIES,
  65. &value, 4);
  66. if (err < 0)
  67. return err;
  68. dice->clock_caps = be32_to_cpu(value);
  69. } else {
  70. /* this should be supported by any device */
  71. dice->clock_caps = CLOCK_CAP_RATE_44100 |
  72. CLOCK_CAP_RATE_48000 |
  73. CLOCK_CAP_SOURCE_ARX1 |
  74. CLOCK_CAP_SOURCE_INTERNAL;
  75. }
  76. return 0;
  77. }
  78. static void dice_card_strings(struct snd_dice *dice)
  79. {
  80. struct snd_card *card = dice->card;
  81. struct fw_device *dev = fw_parent_device(dice->unit);
  82. char vendor[32], model[32];
  83. unsigned int i;
  84. int err;
  85. strcpy(card->driver, "DICE");
  86. strcpy(card->shortname, "DICE");
  87. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  88. err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
  89. card->shortname,
  90. sizeof(card->shortname));
  91. if (err >= 0) {
  92. /* DICE strings are returned in "always-wrong" endianness */
  93. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  94. for (i = 0; i < sizeof(card->shortname); i += 4)
  95. swab32s((u32 *)&card->shortname[i]);
  96. card->shortname[sizeof(card->shortname) - 1] = '\0';
  97. }
  98. strcpy(vendor, "?");
  99. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  100. strcpy(model, "?");
  101. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  102. snprintf(card->longname, sizeof(card->longname),
  103. "%s %s (serial %u) at %s, S%d",
  104. vendor, model, dev->config_rom[4] & 0x3fffff,
  105. dev_name(&dice->unit->device), 100 << dev->max_speed);
  106. strcpy(card->mixername, "DICE");
  107. }
  108. static void dice_free(struct snd_dice *dice)
  109. {
  110. snd_dice_stream_destroy_duplex(dice);
  111. snd_dice_transaction_destroy(dice);
  112. fw_unit_put(dice->unit);
  113. mutex_destroy(&dice->mutex);
  114. kfree(dice);
  115. }
  116. /*
  117. * This module releases the FireWire unit data after all ALSA character devices
  118. * are released by applications. This is for releasing stream data or finishing
  119. * transactions safely. Thus at returning from .remove(), this module still keep
  120. * references for the unit.
  121. */
  122. static void dice_card_free(struct snd_card *card)
  123. {
  124. dice_free(card->private_data);
  125. }
  126. static void do_registration(struct work_struct *work)
  127. {
  128. struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
  129. int err;
  130. if (dice->registered)
  131. return;
  132. err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
  133. &dice->card);
  134. if (err < 0)
  135. return;
  136. err = snd_dice_transaction_init(dice);
  137. if (err < 0)
  138. goto error;
  139. err = check_clock_caps(dice);
  140. if (err < 0)
  141. goto error;
  142. dice_card_strings(dice);
  143. err = dice->detect_formats(dice);
  144. if (err < 0)
  145. goto error;
  146. err = snd_dice_stream_init_duplex(dice);
  147. if (err < 0)
  148. goto error;
  149. snd_dice_create_proc(dice);
  150. err = snd_dice_create_pcm(dice);
  151. if (err < 0)
  152. goto error;
  153. err = snd_dice_create_midi(dice);
  154. if (err < 0)
  155. goto error;
  156. err = snd_dice_create_hwdep(dice);
  157. if (err < 0)
  158. goto error;
  159. err = snd_card_register(dice->card);
  160. if (err < 0)
  161. goto error;
  162. /*
  163. * After registered, dice instance can be released corresponding to
  164. * releasing the sound card instance.
  165. */
  166. dice->card->private_free = dice_card_free;
  167. dice->card->private_data = dice;
  168. dice->registered = true;
  169. return;
  170. error:
  171. snd_dice_stream_destroy_duplex(dice);
  172. snd_dice_transaction_destroy(dice);
  173. snd_dice_stream_destroy_duplex(dice);
  174. snd_card_free(dice->card);
  175. dev_info(&dice->unit->device,
  176. "Sound card registration failed: %d\n", err);
  177. }
  178. static int dice_probe(struct fw_unit *unit,
  179. const struct ieee1394_device_id *entry)
  180. {
  181. struct snd_dice *dice;
  182. int err;
  183. if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
  184. err = check_dice_category(unit);
  185. if (err < 0)
  186. return -ENODEV;
  187. }
  188. /* Allocate this independent of sound card instance. */
  189. dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
  190. if (dice == NULL)
  191. return -ENOMEM;
  192. dice->unit = fw_unit_get(unit);
  193. dev_set_drvdata(&unit->device, dice);
  194. if (!entry->driver_data) {
  195. dice->detect_formats = snd_dice_stream_detect_current_formats;
  196. } else {
  197. dice->detect_formats =
  198. (snd_dice_detect_formats_t)entry->driver_data;
  199. }
  200. spin_lock_init(&dice->lock);
  201. mutex_init(&dice->mutex);
  202. init_completion(&dice->clock_accepted);
  203. init_waitqueue_head(&dice->hwdep_wait);
  204. /* Allocate and register this sound card later. */
  205. INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
  206. snd_fw_schedule_registration(unit, &dice->dwork);
  207. return 0;
  208. }
  209. static void dice_remove(struct fw_unit *unit)
  210. {
  211. struct snd_dice *dice = dev_get_drvdata(&unit->device);
  212. /*
  213. * Confirm to stop the work for registration before the sound card is
  214. * going to be released. The work is not scheduled again because bus
  215. * reset handler is not called anymore.
  216. */
  217. cancel_delayed_work_sync(&dice->dwork);
  218. if (dice->registered) {
  219. /* No need to wait for releasing card object in this context. */
  220. snd_card_free_when_closed(dice->card);
  221. } else {
  222. /* Don't forget this case. */
  223. dice_free(dice);
  224. }
  225. }
  226. static void dice_bus_reset(struct fw_unit *unit)
  227. {
  228. struct snd_dice *dice = dev_get_drvdata(&unit->device);
  229. /* Postpone a workqueue for deferred registration. */
  230. if (!dice->registered)
  231. snd_fw_schedule_registration(unit, &dice->dwork);
  232. /* The handler address register becomes initialized. */
  233. snd_dice_transaction_reinit(dice);
  234. /*
  235. * After registration, userspace can start packet streaming, then this
  236. * code block works fine.
  237. */
  238. if (dice->registered) {
  239. mutex_lock(&dice->mutex);
  240. snd_dice_stream_update_duplex(dice);
  241. mutex_unlock(&dice->mutex);
  242. }
  243. }
  244. #define DICE_INTERFACE 0x000001
  245. static const struct ieee1394_device_id dice_id_table[] = {
  246. /* M-Audio Profire 2626 has a different value in version field. */
  247. {
  248. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  249. IEEE1394_MATCH_MODEL_ID,
  250. .vendor_id = OUI_MAUDIO,
  251. .model_id = 0x000010,
  252. .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
  253. },
  254. /* M-Audio Profire 610 has a different value in version field. */
  255. {
  256. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  257. IEEE1394_MATCH_MODEL_ID,
  258. .vendor_id = OUI_MAUDIO,
  259. .model_id = 0x000011,
  260. .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
  261. },
  262. /* TC Electronic Konnekt 24D. */
  263. {
  264. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  265. IEEE1394_MATCH_MODEL_ID,
  266. .vendor_id = OUI_TCELECTRONIC,
  267. .model_id = 0x000020,
  268. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  269. },
  270. /* TC Electronic Konnekt 8. */
  271. {
  272. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  273. IEEE1394_MATCH_MODEL_ID,
  274. .vendor_id = OUI_TCELECTRONIC,
  275. .model_id = 0x000021,
  276. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  277. },
  278. /* TC Electronic Studio Konnekt 48. */
  279. {
  280. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  281. IEEE1394_MATCH_MODEL_ID,
  282. .vendor_id = OUI_TCELECTRONIC,
  283. .model_id = 0x000022,
  284. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  285. },
  286. /* TC Electronic Konnekt Live. */
  287. {
  288. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  289. IEEE1394_MATCH_MODEL_ID,
  290. .vendor_id = OUI_TCELECTRONIC,
  291. .model_id = 0x000023,
  292. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  293. },
  294. /* TC Electronic Desktop Konnekt 6. */
  295. {
  296. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  297. IEEE1394_MATCH_MODEL_ID,
  298. .vendor_id = OUI_TCELECTRONIC,
  299. .model_id = 0x000024,
  300. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  301. },
  302. /* TC Electronic Impact Twin. */
  303. {
  304. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  305. IEEE1394_MATCH_MODEL_ID,
  306. .vendor_id = OUI_TCELECTRONIC,
  307. .model_id = 0x000027,
  308. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  309. },
  310. /* TC Electronic Digital Konnekt x32. */
  311. {
  312. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  313. IEEE1394_MATCH_MODEL_ID,
  314. .vendor_id = OUI_TCELECTRONIC,
  315. .model_id = 0x000030,
  316. .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
  317. },
  318. /* Alesis iO14/iO26. */
  319. {
  320. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  321. IEEE1394_MATCH_MODEL_ID,
  322. .vendor_id = OUI_ALESIS,
  323. .model_id = MODEL_ALESIS_IO_BOTH,
  324. .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
  325. },
  326. /* Mytek Stereo 192 DSD-DAC. */
  327. {
  328. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  329. IEEE1394_MATCH_MODEL_ID,
  330. .vendor_id = OUI_MYTEK,
  331. .model_id = 0x000002,
  332. .driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
  333. },
  334. // Solid State Logic, Duende Classic and Mini.
  335. // NOTE: each field of GUID in config ROM is not compliant to standard
  336. // DICE scheme.
  337. {
  338. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  339. IEEE1394_MATCH_MODEL_ID,
  340. .vendor_id = OUI_SSL,
  341. .model_id = 0x000070,
  342. },
  343. {
  344. .match_flags = IEEE1394_MATCH_VERSION,
  345. .version = DICE_INTERFACE,
  346. },
  347. { }
  348. };
  349. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  350. static struct fw_driver dice_driver = {
  351. .driver = {
  352. .owner = THIS_MODULE,
  353. .name = KBUILD_MODNAME,
  354. .bus = &fw_bus_type,
  355. },
  356. .probe = dice_probe,
  357. .update = dice_bus_reset,
  358. .remove = dice_remove,
  359. .id_table = dice_id_table,
  360. };
  361. static int __init alsa_dice_init(void)
  362. {
  363. return driver_register(&dice_driver.driver);
  364. }
  365. static void __exit alsa_dice_exit(void)
  366. {
  367. driver_unregister(&dice_driver.driver);
  368. }
  369. module_init(alsa_dice_init);
  370. module_exit(alsa_dice_exit);