dice.c 9.0 KB

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