digi00x.c 5.3 KB

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