dice.c 10 KB

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