chip.c 7.3 KB

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