chip.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux driver for M2Tech hiFace compatible devices
  4. *
  5. * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
  6. *
  7. * Authors: Michael Trimarchi <[email protected]>
  8. * Antonio Ospite <[email protected]>
  9. *
  10. * The driver is based on the work done in TerraTec DMX 6Fire USB
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <sound/initval.h>
  15. #include "chip.h"
  16. #include "pcm.h"
  17. MODULE_AUTHOR("Michael Trimarchi <[email protected]>");
  18. MODULE_AUTHOR("Antonio Ospite <[email protected]>");
  19. MODULE_DESCRIPTION("M2Tech hiFace USB-SPDIF audio driver");
  20. MODULE_LICENSE("GPL v2");
  21. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
  22. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
  23. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  24. #define DRIVER_NAME "snd-usb-hiface"
  25. #define CARD_NAME "hiFace"
  26. module_param_array(index, int, NULL, 0444);
  27. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  28. module_param_array(id, charp, NULL, 0444);
  29. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  30. module_param_array(enable, bool, NULL, 0444);
  31. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  32. static DEFINE_MUTEX(register_mutex);
  33. struct hiface_vendor_quirk {
  34. const char *device_name;
  35. u8 extra_freq;
  36. };
  37. static int hiface_chip_create(struct usb_interface *intf,
  38. struct usb_device *device, int idx,
  39. const struct hiface_vendor_quirk *quirk,
  40. struct hiface_chip **rchip)
  41. {
  42. struct snd_card *card = NULL;
  43. struct hiface_chip *chip;
  44. int ret;
  45. int len;
  46. *rchip = NULL;
  47. /* if we are here, card can be registered in alsa. */
  48. ret = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
  49. sizeof(*chip), &card);
  50. if (ret < 0) {
  51. dev_err(&device->dev, "cannot create alsa card.\n");
  52. return ret;
  53. }
  54. strscpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  55. if (quirk && quirk->device_name)
  56. strscpy(card->shortname, quirk->device_name, sizeof(card->shortname));
  57. else
  58. strscpy(card->shortname, "M2Tech generic audio", sizeof(card->shortname));
  59. strlcat(card->longname, card->shortname, sizeof(card->longname));
  60. len = strlcat(card->longname, " at ", sizeof(card->longname));
  61. if (len < sizeof(card->longname))
  62. usb_make_path(device, card->longname + len,
  63. sizeof(card->longname) - len);
  64. chip = card->private_data;
  65. chip->dev = device;
  66. chip->card = card;
  67. *rchip = chip;
  68. return 0;
  69. }
  70. static int hiface_chip_probe(struct usb_interface *intf,
  71. const struct usb_device_id *usb_id)
  72. {
  73. const struct hiface_vendor_quirk *quirk = (struct hiface_vendor_quirk *)usb_id->driver_info;
  74. int ret;
  75. int i;
  76. struct hiface_chip *chip;
  77. struct usb_device *device = interface_to_usbdev(intf);
  78. ret = usb_set_interface(device, 0, 0);
  79. if (ret != 0) {
  80. dev_err(&device->dev, "can't set first interface for " CARD_NAME " device.\n");
  81. return -EIO;
  82. }
  83. /* check whether the card is already registered */
  84. chip = NULL;
  85. mutex_lock(&register_mutex);
  86. for (i = 0; i < SNDRV_CARDS; i++)
  87. if (enable[i])
  88. break;
  89. if (i >= SNDRV_CARDS) {
  90. dev_err(&device->dev, "no available " CARD_NAME " audio device\n");
  91. ret = -ENODEV;
  92. goto err;
  93. }
  94. ret = hiface_chip_create(intf, device, i, quirk, &chip);
  95. if (ret < 0)
  96. goto err;
  97. ret = hiface_pcm_init(chip, quirk ? quirk->extra_freq : 0);
  98. if (ret < 0)
  99. goto err_chip_destroy;
  100. ret = snd_card_register(chip->card);
  101. if (ret < 0) {
  102. dev_err(&device->dev, "cannot register " CARD_NAME " card\n");
  103. goto err_chip_destroy;
  104. }
  105. mutex_unlock(&register_mutex);
  106. usb_set_intfdata(intf, chip);
  107. return 0;
  108. err_chip_destroy:
  109. snd_card_free(chip->card);
  110. err:
  111. mutex_unlock(&register_mutex);
  112. return ret;
  113. }
  114. static void hiface_chip_disconnect(struct usb_interface *intf)
  115. {
  116. struct hiface_chip *chip;
  117. struct snd_card *card;
  118. chip = usb_get_intfdata(intf);
  119. if (!chip)
  120. return;
  121. card = chip->card;
  122. /* Make sure that the userspace cannot create new request */
  123. snd_card_disconnect(card);
  124. hiface_pcm_abort(chip);
  125. snd_card_free_when_closed(card);
  126. }
  127. static const struct usb_device_id device_table[] = {
  128. {
  129. USB_DEVICE(0x04b4, 0x0384),
  130. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  131. .device_name = "Young",
  132. .extra_freq = 1,
  133. }
  134. },
  135. {
  136. USB_DEVICE(0x04b4, 0x930b),
  137. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  138. .device_name = "hiFace",
  139. }
  140. },
  141. {
  142. USB_DEVICE(0x04b4, 0x931b),
  143. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  144. .device_name = "North Star",
  145. }
  146. },
  147. {
  148. USB_DEVICE(0x04b4, 0x931c),
  149. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  150. .device_name = "W4S Young",
  151. }
  152. },
  153. {
  154. USB_DEVICE(0x04b4, 0x931d),
  155. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  156. .device_name = "Corrson",
  157. }
  158. },
  159. {
  160. USB_DEVICE(0x04b4, 0x931e),
  161. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  162. .device_name = "AUDIA",
  163. }
  164. },
  165. {
  166. USB_DEVICE(0x04b4, 0x931f),
  167. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  168. .device_name = "SL Audio",
  169. }
  170. },
  171. {
  172. USB_DEVICE(0x04b4, 0x9320),
  173. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  174. .device_name = "Empirical",
  175. }
  176. },
  177. {
  178. USB_DEVICE(0x04b4, 0x9321),
  179. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  180. .device_name = "Rockna",
  181. }
  182. },
  183. {
  184. USB_DEVICE(0x249c, 0x9001),
  185. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  186. .device_name = "Pathos",
  187. }
  188. },
  189. {
  190. USB_DEVICE(0x249c, 0x9002),
  191. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  192. .device_name = "Metronome",
  193. }
  194. },
  195. {
  196. USB_DEVICE(0x249c, 0x9006),
  197. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  198. .device_name = "CAD",
  199. }
  200. },
  201. {
  202. USB_DEVICE(0x249c, 0x9008),
  203. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  204. .device_name = "Audio Esclusive",
  205. }
  206. },
  207. {
  208. USB_DEVICE(0x249c, 0x931c),
  209. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  210. .device_name = "Rotel",
  211. }
  212. },
  213. {
  214. USB_DEVICE(0x249c, 0x932c),
  215. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  216. .device_name = "Eeaudio",
  217. }
  218. },
  219. {
  220. USB_DEVICE(0x245f, 0x931c),
  221. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  222. .device_name = "CHORD",
  223. }
  224. },
  225. {
  226. USB_DEVICE(0x25c6, 0x9002),
  227. .driver_info = (unsigned long)&(const struct hiface_vendor_quirk) {
  228. .device_name = "Vitus",
  229. }
  230. },
  231. {}
  232. };
  233. MODULE_DEVICE_TABLE(usb, device_table);
  234. static struct usb_driver hiface_usb_driver = {
  235. .name = DRIVER_NAME,
  236. .probe = hiface_chip_probe,
  237. .disconnect = hiface_chip_disconnect,
  238. .id_table = device_table,
  239. };
  240. module_usb_driver(hiface_usb_driver);