dice.c 10 KB

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