motu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_VERSION:
  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:%d), 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_card_free(struct snd_card *card)
  48. {
  49. struct snd_motu *motu = card->private_data;
  50. snd_motu_transaction_unregister(motu);
  51. snd_motu_stream_destroy_duplex(motu);
  52. }
  53. static void do_registration(struct work_struct *work)
  54. {
  55. struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work);
  56. int err;
  57. if (motu->registered)
  58. return;
  59. err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
  60. &motu->card);
  61. if (err < 0)
  62. return;
  63. motu->card->private_free = motu_card_free;
  64. motu->card->private_data = motu;
  65. name_card(motu);
  66. err = snd_motu_transaction_register(motu);
  67. if (err < 0)
  68. goto error;
  69. err = snd_motu_stream_init_duplex(motu);
  70. if (err < 0)
  71. goto error;
  72. snd_motu_proc_init(motu);
  73. err = snd_motu_create_pcm_devices(motu);
  74. if (err < 0)
  75. goto error;
  76. if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
  77. (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) ||
  78. (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
  79. (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) {
  80. err = snd_motu_create_midi_devices(motu);
  81. if (err < 0)
  82. goto error;
  83. }
  84. err = snd_motu_create_hwdep_device(motu);
  85. if (err < 0)
  86. goto error;
  87. err = snd_card_register(motu->card);
  88. if (err < 0)
  89. goto error;
  90. motu->registered = true;
  91. return;
  92. error:
  93. snd_card_free(motu->card);
  94. dev_info(&motu->unit->device,
  95. "Sound card registration failed: %d\n", err);
  96. }
  97. static int motu_probe(struct fw_unit *unit,
  98. const struct ieee1394_device_id *entry)
  99. {
  100. struct snd_motu *motu;
  101. /* Allocate this independently of sound card instance. */
  102. motu = devm_kzalloc(&unit->device, sizeof(struct snd_motu), GFP_KERNEL);
  103. if (!motu)
  104. return -ENOMEM;
  105. motu->unit = fw_unit_get(unit);
  106. dev_set_drvdata(&unit->device, motu);
  107. motu->spec = (const struct snd_motu_spec *)entry->driver_data;
  108. mutex_init(&motu->mutex);
  109. spin_lock_init(&motu->lock);
  110. init_waitqueue_head(&motu->hwdep_wait);
  111. /* Allocate and register this sound card later. */
  112. INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
  113. snd_fw_schedule_registration(unit, &motu->dwork);
  114. return 0;
  115. }
  116. static void motu_remove(struct fw_unit *unit)
  117. {
  118. struct snd_motu *motu = dev_get_drvdata(&unit->device);
  119. /*
  120. * Confirm to stop the work for registration before the sound card is
  121. * going to be released. The work is not scheduled again because bus
  122. * reset handler is not called anymore.
  123. */
  124. cancel_delayed_work_sync(&motu->dwork);
  125. if (motu->registered) {
  126. // Block till all of ALSA character devices are released.
  127. snd_card_free(motu->card);
  128. }
  129. mutex_destroy(&motu->mutex);
  130. fw_unit_put(motu->unit);
  131. }
  132. static void motu_bus_update(struct fw_unit *unit)
  133. {
  134. struct snd_motu *motu = dev_get_drvdata(&unit->device);
  135. /* Postpone a workqueue for deferred registration. */
  136. if (!motu->registered)
  137. snd_fw_schedule_registration(unit, &motu->dwork);
  138. /* The handler address register becomes initialized. */
  139. snd_motu_transaction_reregister(motu);
  140. }
  141. static const struct snd_motu_spec motu_828mk2 = {
  142. .name = "828mk2",
  143. .protocol = &snd_motu_protocol_v2,
  144. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  145. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  146. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  147. SND_MOTU_SPEC_RX_SEPARETED_MAIN |
  148. SND_MOTU_SPEC_HAS_OPT_IFACE_A |
  149. SND_MOTU_SPEC_RX_MIDI_2ND_Q |
  150. SND_MOTU_SPEC_TX_MIDI_2ND_Q,
  151. .analog_in_ports = 8,
  152. .analog_out_ports = 8,
  153. };
  154. const struct snd_motu_spec snd_motu_spec_traveler = {
  155. .name = "Traveler",
  156. .protocol = &snd_motu_protocol_v2,
  157. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  158. SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
  159. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  160. SND_MOTU_SPEC_HAS_AESEBU_IFACE |
  161. SND_MOTU_SPEC_HAS_OPT_IFACE_A |
  162. SND_MOTU_SPEC_RX_MIDI_2ND_Q |
  163. SND_MOTU_SPEC_TX_MIDI_2ND_Q,
  164. .analog_in_ports = 8,
  165. .analog_out_ports = 8,
  166. };
  167. static const struct snd_motu_spec motu_828mk3 = {
  168. .name = "828mk3",
  169. .protocol = &snd_motu_protocol_v3,
  170. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  171. SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
  172. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  173. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  174. SND_MOTU_SPEC_TX_REVERB_CHUNK |
  175. SND_MOTU_SPEC_RX_SEPARETED_MAIN |
  176. SND_MOTU_SPEC_HAS_OPT_IFACE_A |
  177. SND_MOTU_SPEC_HAS_OPT_IFACE_B |
  178. SND_MOTU_SPEC_RX_MIDI_3RD_Q |
  179. SND_MOTU_SPEC_TX_MIDI_3RD_Q,
  180. .analog_in_ports = 8,
  181. .analog_out_ports = 8,
  182. };
  183. static const struct snd_motu_spec motu_audio_express = {
  184. .name = "AudioExpress",
  185. .protocol = &snd_motu_protocol_v3,
  186. .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
  187. SND_MOTU_SPEC_TX_MICINST_CHUNK |
  188. SND_MOTU_SPEC_TX_RETURN_CHUNK |
  189. SND_MOTU_SPEC_RX_SEPARETED_MAIN |
  190. SND_MOTU_SPEC_RX_MIDI_2ND_Q |
  191. SND_MOTU_SPEC_TX_MIDI_3RD_Q,
  192. .analog_in_ports = 2,
  193. .analog_out_ports = 4,
  194. };
  195. #define SND_MOTU_DEV_ENTRY(model, data) \
  196. { \
  197. .match_flags = IEEE1394_MATCH_VENDOR_ID | \
  198. IEEE1394_MATCH_MODEL_ID | \
  199. IEEE1394_MATCH_SPECIFIER_ID, \
  200. .vendor_id = OUI_MOTU, \
  201. .model_id = model, \
  202. .specifier_id = OUI_MOTU, \
  203. .driver_data = (kernel_ulong_t)data, \
  204. }
  205. static const struct ieee1394_device_id motu_id_table[] = {
  206. SND_MOTU_DEV_ENTRY(0x101800, &motu_828mk2),
  207. SND_MOTU_DEV_ENTRY(0x107800, &snd_motu_spec_traveler),
  208. SND_MOTU_DEV_ENTRY(0x106800, &motu_828mk3), /* FireWire only. */
  209. SND_MOTU_DEV_ENTRY(0x100800, &motu_828mk3), /* Hybrid. */
  210. SND_MOTU_DEV_ENTRY(0x104800, &motu_audio_express),
  211. { }
  212. };
  213. MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
  214. static struct fw_driver motu_driver = {
  215. .driver = {
  216. .owner = THIS_MODULE,
  217. .name = KBUILD_MODNAME,
  218. .bus = &fw_bus_type,
  219. },
  220. .probe = motu_probe,
  221. .update = motu_bus_update,
  222. .remove = motu_remove,
  223. .id_table = motu_id_table,
  224. };
  225. static int __init alsa_motu_init(void)
  226. {
  227. return driver_register(&motu_driver.driver);
  228. }
  229. static void __exit alsa_motu_exit(void)
  230. {
  231. driver_unregister(&motu_driver.driver);
  232. }
  233. module_init(alsa_motu_init);
  234. module_exit(alsa_motu_exit);