motu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * motu.c - a part of driver for MOTU FireWire series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "motu.h"
  9. #define OUI_MOTU 0x0001f2
  10. MODULE_DESCRIPTION("MOTU FireWire driver");
  11. MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
  12. MODULE_LICENSE("GPL v2");
  13. const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = {
  14. /* mode 0 */
  15. [0] = 44100,
  16. [1] = 48000,
  17. /* mode 1 */
  18. [2] = 88200,
  19. [3] = 96000,
  20. /* mode 2 */
  21. [4] = 176400,
  22. [5] = 192000,
  23. };
  24. static void name_card(struct snd_motu *motu)
  25. {
  26. struct fw_device *fw_dev = fw_parent_device(motu->unit);
  27. struct fw_csr_iterator it;
  28. int key, val;
  29. u32 version = 0;
  30. fw_csr_iterator_init(&it, motu->unit->directory);
  31. while (fw_csr_iterator_next(&it, &key, &val)) {
  32. switch (key) {
  33. case CSR_MODEL:
  34. version = val;
  35. break;
  36. }
  37. }
  38. strcpy(motu->card->driver, "FW-MOTU");
  39. strcpy(motu->card->shortname, motu->spec->name);
  40. strcpy(motu->card->mixername, motu->spec->name);
  41. snprintf(motu->card->longname, sizeof(motu->card->longname),
  42. "MOTU %s (version:%06x), GUID %08x%08x at %s, S%d",
  43. motu->spec->name, version,
  44. fw_dev->config_rom[3], fw_dev->config_rom[4],
  45. dev_name(&motu->unit->device), 100 << fw_dev->max_speed);
  46. }
  47. static void motu_free(struct snd_motu *motu)
  48. {
  49. snd_motu_transaction_unregister(motu);
  50. snd_motu_stream_destroy_duplex(motu);
  51. fw_unit_put(motu->unit);
  52. mutex_destroy(&motu->mutex);
  53. kfree(motu);
  54. }
  55. /*
  56. * This module releases the FireWire unit data after all ALSA character devices
  57. * are released by applications. This is for releasing stream data or finishing
  58. * transactions safely. Thus at returning from .remove(), this module still keep
  59. * references for the unit.
  60. */
  61. static void motu_card_free(struct snd_card *card)
  62. {
  63. motu_free(card->private_data);
  64. }
  65. static void do_registration(struct work_struct *work)
  66. {
  67. struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work);
  68. int err;
  69. if (motu->registered)
  70. return;
  71. err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
  72. &motu->card);
  73. if (err < 0)
  74. return;
  75. name_card(motu);
  76. err = snd_motu_transaction_register(motu);
  77. if (err < 0)
  78. goto error;
  79. err = snd_motu_stream_init_duplex(motu);
  80. if (err < 0)
  81. goto error;
  82. snd_motu_proc_init(motu);
  83. err = snd_motu_create_pcm_devices(motu);
  84. if (err < 0)
  85. goto error;
  86. if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
  87. (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) ||
  88. (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
  89. (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) {
  90. err = snd_motu_create_midi_devices(motu);
  91. if (err < 0)
  92. goto error;
  93. }
  94. err = snd_motu_create_hwdep_device(motu);
  95. if (err < 0)
  96. goto error;
  97. err = snd_card_register(motu->card);
  98. if (err < 0)
  99. goto error;
  100. /*
  101. * After registered, motu instance can be released corresponding to
  102. * releasing the sound card instance.
  103. */
  104. motu->card->private_free = motu_card_free;
  105. motu->card->private_data = motu;
  106. motu->registered = true;
  107. return;
  108. error:
  109. snd_motu_transaction_unregister(motu);
  110. snd_motu_stream_destroy_duplex(motu);
  111. snd_card_free(motu->card);
  112. dev_info(&motu->unit->device,
  113. "Sound card registration failed: %d\n", err);
  114. }
  115. static int motu_probe(struct fw_unit *unit,
  116. const struct ieee1394_device_id *entry)
  117. {
  118. struct snd_motu *motu;
  119. /* Allocate this independently of sound card instance. */
  120. motu = kzalloc(sizeof(struct snd_motu), GFP_KERNEL);
  121. if (motu == NULL)
  122. return -ENOMEM;
  123. motu->spec = (const struct snd_motu_spec *)entry->driver_data;
  124. motu->unit = fw_unit_get(unit);
  125. dev_set_drvdata(&unit->device, motu);
  126. mutex_init(&motu->mutex);
  127. spin_lock_init(&motu->lock);
  128. init_waitqueue_head(&motu->hwdep_wait);
  129. /* Allocate and register this sound card later. */
  130. INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
  131. snd_fw_schedule_registration(unit, &motu->dwork);
  132. return 0;
  133. }
  134. static void motu_remove(struct fw_unit *unit)
  135. {
  136. struct snd_motu *motu = dev_get_drvdata(&unit->device);
  137. /*
  138. * Confirm to stop the work for registration before the sound card is
  139. * going to be released. The work is not scheduled again because bus
  140. * reset handler is not called anymore.
  141. */
  142. cancel_delayed_work_sync(&motu->dwork);
  143. if (motu->registered) {
  144. /* No need to wait for releasing card object in this context. */
  145. snd_card_free_when_closed(motu->card);
  146. } else {
  147. /* Don't forget this case. */
  148. motu_free(motu);
  149. }
  150. }
  151. static void motu_bus_update(struct fw_unit *unit)
  152. {
  153. struct snd_motu *motu = dev_get_drvdata(&unit->device);
  154. /* Postpone a workqueue for deferred registration. */
  155. if (!motu->registered)
  156. snd_fw_schedule_registration(unit, &motu->dwork);
  157. /* The handler address register becomes initialized. */
  158. snd_motu_transaction_reregister(motu);
  159. }
  160. static const struct snd_motu_spec motu_828mk2 = {
  161. .name = "828mk2",
  162. .protocol = &snd_motu_protocol_v2,
  163. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  164. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  165. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  166. SND_MOTU_SPEC_RX_SEPARETED_MAIN |
  167. SND_MOTU_SPEC_HAS_OPT_IFACE_A |
  168. SND_MOTU_SPEC_RX_MIDI_2ND_Q |
  169. SND_MOTU_SPEC_TX_MIDI_2ND_Q,
  170. .analog_in_ports = 8,
  171. .analog_out_ports = 8,
  172. };
  173. const struct snd_motu_spec snd_motu_spec_traveler = {
  174. .name = "Traveler",
  175. .protocol = &snd_motu_protocol_v2,
  176. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  177. SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
  178. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  179. SND_MOTU_SPEC_HAS_AESEBU_IFACE |
  180. SND_MOTU_SPEC_HAS_OPT_IFACE_A |
  181. SND_MOTU_SPEC_RX_MIDI_2ND_Q |
  182. SND_MOTU_SPEC_TX_MIDI_2ND_Q,
  183. .analog_in_ports = 8,
  184. .analog_out_ports = 8,
  185. };
  186. static const struct snd_motu_spec motu_828mk3 = {
  187. .name = "828mk3",
  188. .protocol = &snd_motu_protocol_v3,
  189. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  190. SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
  191. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  192. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  193. SND_MOTU_SPEC_TX_REVERB_CHUNK |
  194. SND_MOTU_SPEC_RX_SEPARETED_MAIN |
  195. SND_MOTU_SPEC_HAS_OPT_IFACE_A |
  196. SND_MOTU_SPEC_HAS_OPT_IFACE_B |
  197. SND_MOTU_SPEC_RX_MIDI_3RD_Q |
  198. SND_MOTU_SPEC_TX_MIDI_3RD_Q,
  199. .analog_in_ports = 8,
  200. .analog_out_ports = 8,
  201. };
  202. static const struct snd_motu_spec motu_audio_express = {
  203. .name = "AudioExpress",
  204. .protocol = &snd_motu_protocol_v3,
  205. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  206. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  207. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  208. SND_MOTU_SPEC_RX_SEPARETED_MAIN |
  209. SND_MOTU_SPEC_RX_MIDI_2ND_Q |
  210. SND_MOTU_SPEC_TX_MIDI_3RD_Q,
  211. .analog_in_ports = 2,
  212. .analog_out_ports = 4,
  213. };
  214. static const struct snd_motu_spec motu_4pre = {
  215. .name = "4pre",
  216. .protocol = &snd_motu_protocol_v3,
  217. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  218. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  219. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  220. SND_MOTU_SPEC_RX_SEPARETED_MAIN,
  221. .analog_in_ports = 2,
  222. .analog_out_ports = 2,
  223. };
  224. #define SND_MOTU_DEV_ENTRY(model, data) \
  225. { \
  226. .match_flags = IEEE1394_MATCH_VENDOR_ID | \
  227. IEEE1394_MATCH_SPECIFIER_ID | \
  228. IEEE1394_MATCH_VERSION, \
  229. .vendor_id = OUI_MOTU, \
  230. .specifier_id = OUI_MOTU, \
  231. .version = model, \
  232. .driver_data = (kernel_ulong_t)data, \
  233. }
  234. static const struct ieee1394_device_id motu_id_table[] = {
  235. SND_MOTU_DEV_ENTRY(0x000003, &motu_828mk2),
  236. SND_MOTU_DEV_ENTRY(0x000009, &snd_motu_spec_traveler),
  237. SND_MOTU_DEV_ENTRY(0x000015, &motu_828mk3), /* FireWire only. */
  238. SND_MOTU_DEV_ENTRY(0x000035, &motu_828mk3), /* Hybrid. */
  239. SND_MOTU_DEV_ENTRY(0x000033, &motu_audio_express),
  240. SND_MOTU_DEV_ENTRY(0x000045, &motu_4pre),
  241. { }
  242. };
  243. MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
  244. static struct fw_driver motu_driver = {
  245. .driver = {
  246. .owner = THIS_MODULE,
  247. .name = KBUILD_MODNAME,
  248. .bus = &fw_bus_type,
  249. },
  250. .probe = motu_probe,
  251. .update = motu_bus_update,
  252. .remove = motu_remove,
  253. .id_table = motu_id_table,
  254. };
  255. static int __init alsa_motu_init(void)
  256. {
  257. return driver_register(&motu_driver.driver);
  258. }
  259. static void __exit alsa_motu_exit(void)
  260. {
  261. driver_unregister(&motu_driver.driver);
  262. }
  263. module_init(alsa_motu_init);
  264. module_exit(alsa_motu_exit);