chip.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux driver for TerraTec DMX 6Fire USB
  4. *
  5. * Main routines and module definitions.
  6. *
  7. * Author: Torsten Schenk <torsten.schenk@zoho.com>
  8. * Created: Jan 01, 2011
  9. * Copyright: (C) Torsten Schenk
  10. */
  11. #include "chip.h"
  12. #include "firmware.h"
  13. #include "pcm.h"
  14. #include "control.h"
  15. #include "comm.h"
  16. #include "midi.h"
  17. #include <linux/moduleparam.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/gfp.h>
  22. #include <sound/initval.h>
  23. MODULE_AUTHOR("Torsten Schenk <torsten.schenk@zoho.com>");
  24. MODULE_DESCRIPTION("TerraTec DMX 6Fire USB audio driver");
  25. MODULE_LICENSE("GPL v2");
  26. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
  27. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
  28. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */
  29. static struct sfire_chip *chips[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  30. static struct usb_device *devices[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  31. module_param_array(index, int, NULL, 0444);
  32. MODULE_PARM_DESC(index, "Index value for the 6fire sound device");
  33. module_param_array(id, charp, NULL, 0444);
  34. MODULE_PARM_DESC(id, "ID string for the 6fire sound device.");
  35. module_param_array(enable, bool, NULL, 0444);
  36. MODULE_PARM_DESC(enable, "Enable the 6fire sound device.");
  37. static DEFINE_MUTEX(register_mutex);
  38. static void usb6fire_chip_abort(struct sfire_chip *chip)
  39. {
  40. if (chip) {
  41. if (chip->pcm)
  42. usb6fire_pcm_abort(chip);
  43. if (chip->midi)
  44. usb6fire_midi_abort(chip);
  45. if (chip->comm)
  46. usb6fire_comm_abort(chip);
  47. if (chip->control)
  48. usb6fire_control_abort(chip);
  49. if (chip->card) {
  50. snd_card_disconnect(chip->card);
  51. snd_card_free_when_closed(chip->card);
  52. chip->card = NULL;
  53. }
  54. }
  55. }
  56. static void usb6fire_card_free(struct snd_card *card)
  57. {
  58. struct sfire_chip *chip = card->private_data;
  59. if (chip) {
  60. if (chip->pcm)
  61. usb6fire_pcm_destroy(chip);
  62. if (chip->midi)
  63. usb6fire_midi_destroy(chip);
  64. if (chip->comm)
  65. usb6fire_comm_destroy(chip);
  66. if (chip->control)
  67. usb6fire_control_destroy(chip);
  68. }
  69. }
  70. static int usb6fire_chip_probe(struct usb_interface *intf,
  71. const struct usb_device_id *usb_id)
  72. {
  73. int ret;
  74. int i;
  75. struct sfire_chip *chip = NULL;
  76. struct usb_device *device = interface_to_usbdev(intf);
  77. int regidx = -1; /* index in module parameter array */
  78. struct snd_card *card = NULL;
  79. /* look if we already serve this card and return if so */
  80. mutex_lock(&register_mutex);
  81. for (i = 0; i < SNDRV_CARDS; i++) {
  82. if (devices[i] == device) {
  83. if (chips[i])
  84. chips[i]->intf_count++;
  85. usb_set_intfdata(intf, chips[i]);
  86. mutex_unlock(&register_mutex);
  87. return 0;
  88. } else if (!devices[i] && regidx < 0)
  89. regidx = i;
  90. }
  91. if (regidx < 0) {
  92. mutex_unlock(&register_mutex);
  93. dev_err(&intf->dev, "too many cards registered.\n");
  94. return -ENODEV;
  95. }
  96. devices[regidx] = device;
  97. mutex_unlock(&register_mutex);
  98. /* check, if firmware is present on device, upload it if not */
  99. ret = usb6fire_fw_init(intf);
  100. if (ret < 0)
  101. return ret;
  102. else if (ret == FW_NOT_READY) /* firmware update performed */
  103. return 0;
  104. /* if we are here, card can be registered in alsa. */
  105. if (usb_set_interface(device, 0, 0) != 0) {
  106. dev_err(&intf->dev, "can't set first interface.\n");
  107. return -EIO;
  108. }
  109. ret = snd_card_new(&intf->dev, index[regidx], id[regidx],
  110. THIS_MODULE, sizeof(struct sfire_chip), &card);
  111. if (ret < 0) {
  112. dev_err(&intf->dev, "cannot create alsa card.\n");
  113. return ret;
  114. }
  115. strcpy(card->driver, "6FireUSB");
  116. strcpy(card->shortname, "TerraTec DMX6FireUSB");
  117. sprintf(card->longname, "%s at %d:%d", card->shortname,
  118. device->bus->busnum, device->devnum);
  119. chip = card->private_data;
  120. chips[regidx] = chip;
  121. chip->dev = device;
  122. chip->regidx = regidx;
  123. chip->intf_count = 1;
  124. chip->card = card;
  125. card->private_free = usb6fire_card_free;
  126. ret = usb6fire_comm_init(chip);
  127. if (ret < 0)
  128. goto destroy_chip;
  129. ret = usb6fire_midi_init(chip);
  130. if (ret < 0)
  131. goto destroy_chip;
  132. ret = usb6fire_pcm_init(chip);
  133. if (ret < 0)
  134. goto destroy_chip;
  135. ret = usb6fire_control_init(chip);
  136. if (ret < 0)
  137. goto destroy_chip;
  138. ret = snd_card_register(card);
  139. if (ret < 0) {
  140. dev_err(&intf->dev, "cannot register card.");
  141. goto destroy_chip;
  142. }
  143. usb_set_intfdata(intf, chip);
  144. return 0;
  145. destroy_chip:
  146. snd_card_free(card);
  147. return ret;
  148. }
  149. static void usb6fire_chip_disconnect(struct usb_interface *intf)
  150. {
  151. struct sfire_chip *chip;
  152. chip = usb_get_intfdata(intf);
  153. if (chip) { /* if !chip, fw upload has been performed */
  154. chip->intf_count--;
  155. if (!chip->intf_count) {
  156. mutex_lock(&register_mutex);
  157. devices[chip->regidx] = NULL;
  158. chips[chip->regidx] = NULL;
  159. mutex_unlock(&register_mutex);
  160. chip->shutdown = true;
  161. usb6fire_chip_abort(chip);
  162. }
  163. }
  164. }
  165. static const struct usb_device_id device_table[] = {
  166. {
  167. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  168. .idVendor = 0x0ccd,
  169. .idProduct = 0x0080
  170. },
  171. {}
  172. };
  173. MODULE_DEVICE_TABLE(usb, device_table);
  174. static struct usb_driver usb_driver = {
  175. .name = "snd-usb-6fire",
  176. .probe = usb6fire_chip_probe,
  177. .disconnect = usb6fire_chip_disconnect,
  178. .id_table = device_table,
  179. };
  180. module_usb_driver(usb_driver);