chip.c 5.5 KB

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