tascam.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_card_free(struct snd_card *card)
  79. {
  80. struct snd_tscm *tscm = card->private_data;
  81. snd_tscm_transaction_unregister(tscm);
  82. snd_tscm_stream_destroy_duplex(tscm);
  83. }
  84. static void do_registration(struct work_struct *work)
  85. {
  86. struct snd_tscm *tscm = container_of(work, struct snd_tscm, dwork.work);
  87. int err;
  88. err = snd_card_new(&tscm->unit->device, -1, NULL, THIS_MODULE, 0,
  89. &tscm->card);
  90. if (err < 0)
  91. return;
  92. tscm->card->private_free = tscm_card_free;
  93. tscm->card->private_data = tscm;
  94. err = identify_model(tscm);
  95. if (err < 0)
  96. goto error;
  97. err = snd_tscm_transaction_register(tscm);
  98. if (err < 0)
  99. goto error;
  100. err = snd_tscm_stream_init_duplex(tscm);
  101. if (err < 0)
  102. goto error;
  103. snd_tscm_proc_init(tscm);
  104. err = snd_tscm_create_pcm_devices(tscm);
  105. if (err < 0)
  106. goto error;
  107. err = snd_tscm_create_midi_devices(tscm);
  108. if (err < 0)
  109. goto error;
  110. err = snd_tscm_create_hwdep_device(tscm);
  111. if (err < 0)
  112. goto error;
  113. err = snd_card_register(tscm->card);
  114. if (err < 0)
  115. goto error;
  116. tscm->registered = true;
  117. return;
  118. error:
  119. snd_card_free(tscm->card);
  120. dev_info(&tscm->unit->device,
  121. "Sound card registration failed: %d\n", err);
  122. }
  123. static int snd_tscm_probe(struct fw_unit *unit,
  124. const struct ieee1394_device_id *entry)
  125. {
  126. struct snd_tscm *tscm;
  127. /* Allocate this independent of sound card instance. */
  128. tscm = devm_kzalloc(&unit->device, sizeof(struct snd_tscm), GFP_KERNEL);
  129. if (!tscm)
  130. return -ENOMEM;
  131. tscm->unit = fw_unit_get(unit);
  132. dev_set_drvdata(&unit->device, tscm);
  133. mutex_init(&tscm->mutex);
  134. spin_lock_init(&tscm->lock);
  135. init_waitqueue_head(&tscm->hwdep_wait);
  136. /* Allocate and register this sound card later. */
  137. INIT_DEFERRABLE_WORK(&tscm->dwork, do_registration);
  138. snd_fw_schedule_registration(unit, &tscm->dwork);
  139. return 0;
  140. }
  141. static void snd_tscm_update(struct fw_unit *unit)
  142. {
  143. struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
  144. /* Postpone a workqueue for deferred registration. */
  145. if (!tscm->registered)
  146. snd_fw_schedule_registration(unit, &tscm->dwork);
  147. snd_tscm_transaction_reregister(tscm);
  148. /*
  149. * After registration, userspace can start packet streaming, then this
  150. * code block works fine.
  151. */
  152. if (tscm->registered) {
  153. mutex_lock(&tscm->mutex);
  154. snd_tscm_stream_update_duplex(tscm);
  155. mutex_unlock(&tscm->mutex);
  156. }
  157. }
  158. static void snd_tscm_remove(struct fw_unit *unit)
  159. {
  160. struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
  161. /*
  162. * Confirm to stop the work for registration before the sound card is
  163. * going to be released. The work is not scheduled again because bus
  164. * reset handler is not called anymore.
  165. */
  166. cancel_delayed_work_sync(&tscm->dwork);
  167. if (tscm->registered) {
  168. // Block till all of ALSA character devices are released.
  169. snd_card_free(tscm->card);
  170. }
  171. mutex_destroy(&tscm->mutex);
  172. fw_unit_put(tscm->unit);
  173. }
  174. static const struct ieee1394_device_id snd_tscm_id_table[] = {
  175. {
  176. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  177. IEEE1394_MATCH_SPECIFIER_ID,
  178. .vendor_id = 0x00022e,
  179. .specifier_id = 0x00022e,
  180. },
  181. /* FE-08 requires reverse-engineering because it just has faders. */
  182. {}
  183. };
  184. MODULE_DEVICE_TABLE(ieee1394, snd_tscm_id_table);
  185. static struct fw_driver tscm_driver = {
  186. .driver = {
  187. .owner = THIS_MODULE,
  188. .name = "snd-firewire-tascam",
  189. .bus = &fw_bus_type,
  190. },
  191. .probe = snd_tscm_probe,
  192. .update = snd_tscm_update,
  193. .remove = snd_tscm_remove,
  194. .id_table = snd_tscm_id_table,
  195. };
  196. static int __init snd_tscm_init(void)
  197. {
  198. return driver_register(&tscm_driver.driver);
  199. }
  200. static void __exit snd_tscm_exit(void)
  201. {
  202. driver_unregister(&tscm_driver.driver);
  203. }
  204. module_init(snd_tscm_init);
  205. module_exit(snd_tscm_exit);