digi00x.c 4.9 KB

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