tascam.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * tascam.c - a part of driver for TASCAM FireWire series
  3. *
  4. * Copyright (c) 2015 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "tascam.h"
  9. MODULE_DESCRIPTION("TASCAM FireWire series Driver");
  10. MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
  11. MODULE_LICENSE("GPL v2");
  12. static const struct snd_tscm_spec model_specs[] = {
  13. {
  14. .name = "FW-1884",
  15. .has_adat = true,
  16. .has_spdif = true,
  17. .pcm_capture_analog_channels = 8,
  18. .pcm_playback_analog_channels = 8,
  19. .midi_capture_ports = 4,
  20. .midi_playback_ports = 4,
  21. },
  22. {
  23. .name = "FW-1082",
  24. .has_adat = false,
  25. .has_spdif = true,
  26. .pcm_capture_analog_channels = 8,
  27. .pcm_playback_analog_channels = 2,
  28. .midi_capture_ports = 2,
  29. .midi_playback_ports = 2,
  30. },
  31. {
  32. .name = "FW-1804",
  33. .has_adat = true,
  34. .has_spdif = true,
  35. .pcm_capture_analog_channels = 8,
  36. .pcm_playback_analog_channels = 2,
  37. .midi_capture_ports = 2,
  38. .midi_playback_ports = 4,
  39. },
  40. };
  41. static int identify_model(struct snd_tscm *tscm)
  42. {
  43. struct fw_device *fw_dev = fw_parent_device(tscm->unit);
  44. const u32 *config_rom = fw_dev->config_rom;
  45. char model[9];
  46. unsigned int i;
  47. u8 c;
  48. if (fw_dev->config_rom_length < 30) {
  49. dev_err(&tscm->unit->device,
  50. "Configuration ROM is too short.\n");
  51. return -ENODEV;
  52. }
  53. /* Pick up model name from certain addresses. */
  54. for (i = 0; i < 8; i++) {
  55. c = config_rom[28 + i / 4] >> (24 - 8 * (i % 4));
  56. if (c == '\0')
  57. break;
  58. model[i] = c;
  59. }
  60. model[i] = '\0';
  61. for (i = 0; i < ARRAY_SIZE(model_specs); i++) {
  62. if (strcmp(model, model_specs[i].name) == 0) {
  63. tscm->spec = &model_specs[i];
  64. break;
  65. }
  66. }
  67. if (tscm->spec == NULL)
  68. return -ENODEV;
  69. strcpy(tscm->card->driver, "FW-TASCAM");
  70. strcpy(tscm->card->shortname, model);
  71. strcpy(tscm->card->mixername, model);
  72. snprintf(tscm->card->longname, sizeof(tscm->card->longname),
  73. "TASCAM %s, GUID %08x%08x at %s, S%d", model,
  74. fw_dev->config_rom[3], fw_dev->config_rom[4],
  75. dev_name(&tscm->unit->device), 100 << fw_dev->max_speed);
  76. return 0;
  77. }
  78. static void tscm_free(struct snd_tscm *tscm)
  79. {
  80. snd_tscm_transaction_unregister(tscm);
  81. snd_tscm_stream_destroy_duplex(tscm);
  82. fw_unit_put(tscm->unit);
  83. mutex_destroy(&tscm->mutex);
  84. kfree(tscm);
  85. }
  86. static void tscm_card_free(struct snd_card *card)
  87. {
  88. tscm_free(card->private_data);
  89. }
  90. static void do_registration(struct work_struct *work)
  91. {
  92. struct snd_tscm *tscm = container_of(work, struct snd_tscm, dwork.work);
  93. int err;
  94. err = snd_card_new(&tscm->unit->device, -1, NULL, THIS_MODULE, 0,
  95. &tscm->card);
  96. if (err < 0)
  97. return;
  98. err = identify_model(tscm);
  99. if (err < 0)
  100. goto error;
  101. err = snd_tscm_transaction_register(tscm);
  102. if (err < 0)
  103. goto error;
  104. err = snd_tscm_stream_init_duplex(tscm);
  105. if (err < 0)
  106. goto error;
  107. snd_tscm_proc_init(tscm);
  108. err = snd_tscm_create_pcm_devices(tscm);
  109. if (err < 0)
  110. goto error;
  111. err = snd_tscm_create_midi_devices(tscm);
  112. if (err < 0)
  113. goto error;
  114. err = snd_tscm_create_hwdep_device(tscm);
  115. if (err < 0)
  116. goto error;
  117. err = snd_card_register(tscm->card);
  118. if (err < 0)
  119. goto error;
  120. /*
  121. * After registered, tscm instance can be released corresponding to
  122. * releasing the sound card instance.
  123. */
  124. tscm->card->private_free = tscm_card_free;
  125. tscm->card->private_data = tscm;
  126. tscm->registered = true;
  127. return;
  128. error:
  129. snd_tscm_transaction_unregister(tscm);
  130. snd_tscm_stream_destroy_duplex(tscm);
  131. snd_card_free(tscm->card);
  132. dev_info(&tscm->unit->device,
  133. "Sound card registration failed: %d\n", err);
  134. }
  135. static int snd_tscm_probe(struct fw_unit *unit,
  136. const struct ieee1394_device_id *entry)
  137. {
  138. struct snd_tscm *tscm;
  139. /* Allocate this independent of sound card instance. */
  140. tscm = kzalloc(sizeof(struct snd_tscm), GFP_KERNEL);
  141. if (tscm == NULL)
  142. return -ENOMEM;
  143. /* initialize myself */
  144. tscm->unit = fw_unit_get(unit);
  145. dev_set_drvdata(&unit->device, tscm);
  146. mutex_init(&tscm->mutex);
  147. spin_lock_init(&tscm->lock);
  148. init_waitqueue_head(&tscm->hwdep_wait);
  149. /* Allocate and register this sound card later. */
  150. INIT_DEFERRABLE_WORK(&tscm->dwork, do_registration);
  151. snd_fw_schedule_registration(unit, &tscm->dwork);
  152. return 0;
  153. }
  154. static void snd_tscm_update(struct fw_unit *unit)
  155. {
  156. struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
  157. /* Postpone a workqueue for deferred registration. */
  158. if (!tscm->registered)
  159. snd_fw_schedule_registration(unit, &tscm->dwork);
  160. snd_tscm_transaction_reregister(tscm);
  161. /*
  162. * After registration, userspace can start packet streaming, then this
  163. * code block works fine.
  164. */
  165. if (tscm->registered) {
  166. mutex_lock(&tscm->mutex);
  167. snd_tscm_stream_update_duplex(tscm);
  168. mutex_unlock(&tscm->mutex);
  169. }
  170. }
  171. static void snd_tscm_remove(struct fw_unit *unit)
  172. {
  173. struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
  174. /*
  175. * Confirm to stop the work for registration before the sound card is
  176. * going to be released. The work is not scheduled again because bus
  177. * reset handler is not called anymore.
  178. */
  179. cancel_delayed_work_sync(&tscm->dwork);
  180. if (tscm->registered) {
  181. /* No need to wait for releasing card object in this context. */
  182. snd_card_free_when_closed(tscm->card);
  183. } else {
  184. /* Don't forget this case. */
  185. tscm_free(tscm);
  186. }
  187. }
  188. static const struct ieee1394_device_id snd_tscm_id_table[] = {
  189. // Tascam, FW-1884.
  190. {
  191. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  192. IEEE1394_MATCH_SPECIFIER_ID |
  193. IEEE1394_MATCH_VERSION,
  194. .vendor_id = 0x00022e,
  195. .specifier_id = 0x00022e,
  196. .version = 0x800000,
  197. },
  198. // Tascam, FE-8 (.version = 0x800001)
  199. // This kernel module doesn't support FE-8 because the most of features
  200. // can be implemented in userspace without any specific support of this
  201. // module.
  202. //
  203. // .version = 0x800002 is unknown.
  204. //
  205. // Tascam, FW-1082.
  206. {
  207. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  208. IEEE1394_MATCH_SPECIFIER_ID |
  209. IEEE1394_MATCH_VERSION,
  210. .vendor_id = 0x00022e,
  211. .specifier_id = 0x00022e,
  212. .version = 0x800003,
  213. },
  214. // Tascam, FW-1804.
  215. {
  216. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  217. IEEE1394_MATCH_SPECIFIER_ID |
  218. IEEE1394_MATCH_VERSION,
  219. .vendor_id = 0x00022e,
  220. .specifier_id = 0x00022e,
  221. .version = 0x800004,
  222. },
  223. /* FE-08 requires reverse-engineering because it just has faders. */
  224. {}
  225. };
  226. MODULE_DEVICE_TABLE(ieee1394, snd_tscm_id_table);
  227. static struct fw_driver tscm_driver = {
  228. .driver = {
  229. .owner = THIS_MODULE,
  230. .name = "snd-firewire-tascam",
  231. .bus = &fw_bus_type,
  232. },
  233. .probe = snd_tscm_probe,
  234. .update = snd_tscm_update,
  235. .remove = snd_tscm_remove,
  236. .id_table = snd_tscm_id_table,
  237. };
  238. static int __init snd_tscm_init(void)
  239. {
  240. return driver_register(&tscm_driver.driver);
  241. }
  242. static void __exit snd_tscm_exit(void)
  243. {
  244. driver_unregister(&tscm_driver.driver);
  245. }
  246. module_init(snd_tscm_init);
  247. module_exit(snd_tscm_exit);