digi00x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * digi00x.c - a part of driver for Digidesign Digi 002/003 family
  3. *
  4. * Copyright (c) 2014-2015 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "digi00x.h"
  9. MODULE_DESCRIPTION("Digidesign Digi 002/003 family Driver");
  10. MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
  11. MODULE_LICENSE("GPL v2");
  12. #define VENDOR_DIGIDESIGN 0x00a07e
  13. #define MODEL_CONSOLE 0x000001
  14. #define MODEL_RACK 0x000002
  15. static int name_card(struct snd_dg00x *dg00x)
  16. {
  17. struct fw_device *fw_dev = fw_parent_device(dg00x->unit);
  18. char name[32] = {0};
  19. char *model;
  20. int err;
  21. err = fw_csr_string(dg00x->unit->directory, CSR_MODEL, name,
  22. sizeof(name));
  23. if (err < 0)
  24. return err;
  25. model = skip_spaces(name);
  26. strcpy(dg00x->card->driver, "Digi00x");
  27. strcpy(dg00x->card->shortname, model);
  28. strcpy(dg00x->card->mixername, model);
  29. snprintf(dg00x->card->longname, sizeof(dg00x->card->longname),
  30. "Digidesign %s, GUID %08x%08x at %s, S%d", model,
  31. fw_dev->config_rom[3], fw_dev->config_rom[4],
  32. dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
  33. return 0;
  34. }
  35. static void dg00x_free(struct snd_dg00x *dg00x)
  36. {
  37. snd_dg00x_stream_destroy_duplex(dg00x);
  38. snd_dg00x_transaction_unregister(dg00x);
  39. fw_unit_put(dg00x->unit);
  40. mutex_destroy(&dg00x->mutex);
  41. }
  42. static void dg00x_card_free(struct snd_card *card)
  43. {
  44. dg00x_free(card->private_data);
  45. }
  46. static void do_registration(struct work_struct *work)
  47. {
  48. struct snd_dg00x *dg00x =
  49. container_of(work, struct snd_dg00x, dwork.work);
  50. int err;
  51. if (dg00x->registered)
  52. return;
  53. err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
  54. &dg00x->card);
  55. if (err < 0)
  56. return;
  57. err = name_card(dg00x);
  58. if (err < 0)
  59. goto error;
  60. err = snd_dg00x_stream_init_duplex(dg00x);
  61. if (err < 0)
  62. goto error;
  63. snd_dg00x_proc_init(dg00x);
  64. err = snd_dg00x_create_pcm_devices(dg00x);
  65. if (err < 0)
  66. goto error;
  67. err = snd_dg00x_create_midi_devices(dg00x);
  68. if (err < 0)
  69. goto error;
  70. err = snd_dg00x_create_hwdep_device(dg00x);
  71. if (err < 0)
  72. goto error;
  73. err = snd_dg00x_transaction_register(dg00x);
  74. if (err < 0)
  75. goto error;
  76. err = snd_card_register(dg00x->card);
  77. if (err < 0)
  78. goto error;
  79. dg00x->card->private_free = dg00x_card_free;
  80. dg00x->card->private_data = dg00x;
  81. dg00x->registered = true;
  82. return;
  83. error:
  84. snd_dg00x_transaction_unregister(dg00x);
  85. snd_dg00x_stream_destroy_duplex(dg00x);
  86. snd_card_free(dg00x->card);
  87. dev_info(&dg00x->unit->device,
  88. "Sound card registration failed: %d\n", err);
  89. }
  90. static int snd_dg00x_probe(struct fw_unit *unit,
  91. const struct ieee1394_device_id *entry)
  92. {
  93. struct snd_dg00x *dg00x;
  94. /* Allocate this independent of sound card instance. */
  95. dg00x = kzalloc(sizeof(struct snd_dg00x), GFP_KERNEL);
  96. if (dg00x == NULL)
  97. return -ENOMEM;
  98. dg00x->unit = fw_unit_get(unit);
  99. dev_set_drvdata(&unit->device, dg00x);
  100. mutex_init(&dg00x->mutex);
  101. spin_lock_init(&dg00x->lock);
  102. init_waitqueue_head(&dg00x->hwdep_wait);
  103. dg00x->is_console = entry->model_id == MODEL_CONSOLE;
  104. /* Allocate and register this sound card later. */
  105. INIT_DEFERRABLE_WORK(&dg00x->dwork, do_registration);
  106. snd_fw_schedule_registration(unit, &dg00x->dwork);
  107. return 0;
  108. }
  109. static void snd_dg00x_update(struct fw_unit *unit)
  110. {
  111. struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
  112. /* Postpone a workqueue for deferred registration. */
  113. if (!dg00x->registered)
  114. snd_fw_schedule_registration(unit, &dg00x->dwork);
  115. snd_dg00x_transaction_reregister(dg00x);
  116. /*
  117. * After registration, userspace can start packet streaming, then this
  118. * code block works fine.
  119. */
  120. if (dg00x->registered) {
  121. mutex_lock(&dg00x->mutex);
  122. snd_dg00x_stream_update_duplex(dg00x);
  123. mutex_unlock(&dg00x->mutex);
  124. }
  125. }
  126. static void snd_dg00x_remove(struct fw_unit *unit)
  127. {
  128. struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
  129. /*
  130. * Confirm to stop the work for registration before the sound card is
  131. * going to be released. The work is not scheduled again because bus
  132. * reset handler is not called anymore.
  133. */
  134. cancel_delayed_work_sync(&dg00x->dwork);
  135. if (dg00x->registered) {
  136. /* No need to wait for releasing card object in this context. */
  137. snd_card_free_when_closed(dg00x->card);
  138. } else {
  139. /* Don't forget this case. */
  140. dg00x_free(dg00x);
  141. }
  142. }
  143. static const struct ieee1394_device_id snd_dg00x_id_table[] = {
  144. /* Both of 002/003 use the same ID. */
  145. {
  146. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  147. IEEE1394_MATCH_MODEL_ID,
  148. .vendor_id = VENDOR_DIGIDESIGN,
  149. .model_id = MODEL_CONSOLE,
  150. },
  151. {
  152. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  153. IEEE1394_MATCH_MODEL_ID,
  154. .vendor_id = VENDOR_DIGIDESIGN,
  155. .model_id = MODEL_RACK,
  156. },
  157. {}
  158. };
  159. MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
  160. static struct fw_driver dg00x_driver = {
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = "snd-firewire-digi00x",
  164. .bus = &fw_bus_type,
  165. },
  166. .probe = snd_dg00x_probe,
  167. .update = snd_dg00x_update,
  168. .remove = snd_dg00x_remove,
  169. .id_table = snd_dg00x_id_table,
  170. };
  171. static int __init snd_dg00x_init(void)
  172. {
  173. return driver_register(&dg00x_driver.driver);
  174. }
  175. static void __exit snd_dg00x_exit(void)
  176. {
  177. driver_unregister(&dg00x_driver.driver);
  178. }
  179. module_init(snd_dg00x_init);
  180. module_exit(snd_dg00x_exit);