digi00x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. kfree(dg00x);
  42. }
  43. static void dg00x_card_free(struct snd_card *card)
  44. {
  45. dg00x_free(card->private_data);
  46. }
  47. static void do_registration(struct work_struct *work)
  48. {
  49. struct snd_dg00x *dg00x =
  50. container_of(work, struct snd_dg00x, dwork.work);
  51. int err;
  52. if (dg00x->registered)
  53. return;
  54. err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
  55. &dg00x->card);
  56. if (err < 0)
  57. return;
  58. err = name_card(dg00x);
  59. if (err < 0)
  60. goto error;
  61. err = snd_dg00x_stream_init_duplex(dg00x);
  62. if (err < 0)
  63. goto error;
  64. snd_dg00x_proc_init(dg00x);
  65. err = snd_dg00x_create_pcm_devices(dg00x);
  66. if (err < 0)
  67. goto error;
  68. err = snd_dg00x_create_midi_devices(dg00x);
  69. if (err < 0)
  70. goto error;
  71. err = snd_dg00x_create_hwdep_device(dg00x);
  72. if (err < 0)
  73. goto error;
  74. err = snd_dg00x_transaction_register(dg00x);
  75. if (err < 0)
  76. goto error;
  77. err = snd_card_register(dg00x->card);
  78. if (err < 0)
  79. goto error;
  80. dg00x->card->private_free = dg00x_card_free;
  81. dg00x->card->private_data = dg00x;
  82. dg00x->registered = true;
  83. return;
  84. error:
  85. snd_dg00x_transaction_unregister(dg00x);
  86. snd_dg00x_stream_destroy_duplex(dg00x);
  87. snd_card_free(dg00x->card);
  88. dev_info(&dg00x->unit->device,
  89. "Sound card registration failed: %d\n", err);
  90. }
  91. static int snd_dg00x_probe(struct fw_unit *unit,
  92. const struct ieee1394_device_id *entry)
  93. {
  94. struct snd_dg00x *dg00x;
  95. /* Allocate this independent of sound card instance. */
  96. dg00x = kzalloc(sizeof(struct snd_dg00x), GFP_KERNEL);
  97. if (dg00x == NULL)
  98. return -ENOMEM;
  99. dg00x->unit = fw_unit_get(unit);
  100. dev_set_drvdata(&unit->device, dg00x);
  101. mutex_init(&dg00x->mutex);
  102. spin_lock_init(&dg00x->lock);
  103. init_waitqueue_head(&dg00x->hwdep_wait);
  104. dg00x->is_console = entry->model_id == MODEL_CONSOLE;
  105. /* Allocate and register this sound card later. */
  106. INIT_DEFERRABLE_WORK(&dg00x->dwork, do_registration);
  107. snd_fw_schedule_registration(unit, &dg00x->dwork);
  108. return 0;
  109. }
  110. static void snd_dg00x_update(struct fw_unit *unit)
  111. {
  112. struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
  113. /* Postpone a workqueue for deferred registration. */
  114. if (!dg00x->registered)
  115. snd_fw_schedule_registration(unit, &dg00x->dwork);
  116. snd_dg00x_transaction_reregister(dg00x);
  117. /*
  118. * After registration, userspace can start packet streaming, then this
  119. * code block works fine.
  120. */
  121. if (dg00x->registered) {
  122. mutex_lock(&dg00x->mutex);
  123. snd_dg00x_stream_update_duplex(dg00x);
  124. mutex_unlock(&dg00x->mutex);
  125. }
  126. }
  127. static void snd_dg00x_remove(struct fw_unit *unit)
  128. {
  129. struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
  130. /*
  131. * Confirm to stop the work for registration before the sound card is
  132. * going to be released. The work is not scheduled again because bus
  133. * reset handler is not called anymore.
  134. */
  135. cancel_delayed_work_sync(&dg00x->dwork);
  136. if (dg00x->registered) {
  137. /* No need to wait for releasing card object in this context. */
  138. snd_card_free_when_closed(dg00x->card);
  139. } else {
  140. /* Don't forget this case. */
  141. dg00x_free(dg00x);
  142. }
  143. }
  144. static const struct ieee1394_device_id snd_dg00x_id_table[] = {
  145. /* Both of 002/003 use the same ID. */
  146. {
  147. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  148. IEEE1394_MATCH_MODEL_ID,
  149. .vendor_id = VENDOR_DIGIDESIGN,
  150. .model_id = MODEL_CONSOLE,
  151. },
  152. {
  153. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  154. IEEE1394_MATCH_MODEL_ID,
  155. .vendor_id = VENDOR_DIGIDESIGN,
  156. .model_id = MODEL_RACK,
  157. },
  158. {}
  159. };
  160. MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
  161. static struct fw_driver dg00x_driver = {
  162. .driver = {
  163. .owner = THIS_MODULE,
  164. .name = "snd-firewire-digi00x",
  165. .bus = &fw_bus_type,
  166. },
  167. .probe = snd_dg00x_probe,
  168. .update = snd_dg00x_update,
  169. .remove = snd_dg00x_remove,
  170. .id_table = snd_dg00x_id_table,
  171. };
  172. static int __init snd_dg00x_init(void)
  173. {
  174. return driver_register(&dg00x_driver.driver);
  175. }
  176. static void __exit snd_dg00x_exit(void)
  177. {
  178. driver_unregister(&dg00x_driver.driver);
  179. }
  180. module_init(snd_dg00x_init);
  181. module_exit(snd_dg00x_exit);