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 <[email protected]>
  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 <[email protected]>");
  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_chip_destroy(struct sfire_chip *chip)
  57. {
  58. if (chip) {
  59. if (chip->pcm)
  60. usb6fire_pcm_destroy(chip);
  61. if (chip->midi)
  62. usb6fire_midi_destroy(chip);
  63. if (chip->comm)
  64. usb6fire_comm_destroy(chip);
  65. if (chip->control)
  66. usb6fire_control_destroy(chip);
  67. if (chip->card)
  68. snd_card_free(chip->card);
  69. }
  70. }
  71. static int usb6fire_chip_probe(struct usb_interface *intf,
  72. const struct usb_device_id *usb_id)
  73. {
  74. int ret;
  75. int i;
  76. struct sfire_chip *chip = NULL;
  77. struct usb_device *device = interface_to_usbdev(intf);
  78. int regidx = -1; /* index in module parameter array */
  79. struct snd_card *card = NULL;
  80. /* look if we already serve this card and return if so */
  81. mutex_lock(&register_mutex);
  82. for (i = 0; i < SNDRV_CARDS; i++) {
  83. if (devices[i] == device) {
  84. if (chips[i])
  85. chips[i]->intf_count++;
  86. usb_set_intfdata(intf, chips[i]);
  87. mutex_unlock(&register_mutex);
  88. return 0;
  89. } else if (!devices[i] && regidx < 0)
  90. regidx = i;
  91. }
  92. if (regidx < 0) {
  93. mutex_unlock(&register_mutex);
  94. dev_err(&intf->dev, "too many cards registered.\n");
  95. return -ENODEV;
  96. }
  97. devices[regidx] = device;
  98. mutex_unlock(&register_mutex);
  99. /* check, if firmware is present on device, upload it if not */
  100. ret = usb6fire_fw_init(intf);
  101. if (ret < 0)
  102. return ret;
  103. else if (ret == FW_NOT_READY) /* firmware update performed */
  104. return 0;
  105. /* if we are here, card can be registered in alsa. */
  106. if (usb_set_interface(device, 0, 0) != 0) {
  107. dev_err(&intf->dev, "can't set first interface.\n");
  108. return -EIO;
  109. }
  110. ret = snd_card_new(&intf->dev, index[regidx], id[regidx],
  111. THIS_MODULE, sizeof(struct sfire_chip), &card);
  112. if (ret < 0) {
  113. dev_err(&intf->dev, "cannot create alsa card.\n");
  114. return ret;
  115. }
  116. strcpy(card->driver, "6FireUSB");
  117. strcpy(card->shortname, "TerraTec DMX6FireUSB");
  118. sprintf(card->longname, "%s at %d:%d", card->shortname,
  119. device->bus->busnum, device->devnum);
  120. chip = card->private_data;
  121. chips[regidx] = chip;
  122. chip->dev = device;
  123. chip->regidx = regidx;
  124. chip->intf_count = 1;
  125. chip->card = card;
  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. usb6fire_chip_destroy(chip);
  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. usb6fire_chip_destroy(chip);
  163. }
  164. }
  165. }
  166. static const struct usb_device_id device_table[] = {
  167. {
  168. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  169. .idVendor = 0x0ccd,
  170. .idProduct = 0x0080
  171. },
  172. {}
  173. };
  174. MODULE_DEVICE_TABLE(usb, device_table);
  175. static struct usb_driver usb_driver = {
  176. .name = "snd-usb-6fire",
  177. .probe = usb6fire_chip_probe,
  178. .disconnect = usb6fire_chip_disconnect,
  179. .id_table = device_table,
  180. };
  181. module_usb_driver(usb_driver);