mixer_quirks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * USB Audio Driver for ALSA
  3. *
  4. * Quirks and vendor-specific extensions for mixer interfaces
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * Many codes borrowed from audio.c by
  9. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  10. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/usb.h>
  30. #include <linux/usb/audio.h>
  31. #include <sound/core.h>
  32. #include <sound/control.h>
  33. #include <sound/hwdep.h>
  34. #include <sound/info.h>
  35. #include "usbaudio.h"
  36. #include "mixer.h"
  37. #include "mixer_quirks.h"
  38. #include "helper.h"
  39. /*
  40. * Sound Blaster remote control configuration
  41. *
  42. * format of remote control data:
  43. * Extigy: xx 00
  44. * Audigy 2 NX: 06 80 xx 00 00 00
  45. * Live! 24-bit: 06 80 xx yy 22 83
  46. */
  47. static const struct rc_config {
  48. u32 usb_id;
  49. u8 offset;
  50. u8 length;
  51. u8 packet_length;
  52. u8 min_packet_length; /* minimum accepted length of the URB result */
  53. u8 mute_mixer_id;
  54. u32 mute_code;
  55. } rc_configs[] = {
  56. { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
  57. { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
  58. { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
  59. { USB_ID(0x041e, 0x3042), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 */
  60. { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
  61. };
  62. static void snd_usb_soundblaster_remote_complete(struct urb *urb)
  63. {
  64. struct usb_mixer_interface *mixer = urb->context;
  65. const struct rc_config *rc = mixer->rc_cfg;
  66. u32 code;
  67. if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
  68. return;
  69. code = mixer->rc_buffer[rc->offset];
  70. if (rc->length == 2)
  71. code |= mixer->rc_buffer[rc->offset + 1] << 8;
  72. /* the Mute button actually changes the mixer control */
  73. if (code == rc->mute_code)
  74. snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
  75. mixer->rc_code = code;
  76. wmb();
  77. wake_up(&mixer->rc_waitq);
  78. }
  79. static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
  80. long count, loff_t *offset)
  81. {
  82. struct usb_mixer_interface *mixer = hw->private_data;
  83. int err;
  84. u32 rc_code;
  85. if (count != 1 && count != 4)
  86. return -EINVAL;
  87. err = wait_event_interruptible(mixer->rc_waitq,
  88. (rc_code = xchg(&mixer->rc_code, 0)) != 0);
  89. if (err == 0) {
  90. if (count == 1)
  91. err = put_user(rc_code, buf);
  92. else
  93. err = put_user(rc_code, (u32 __user *)buf);
  94. }
  95. return err < 0 ? err : count;
  96. }
  97. static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
  98. poll_table *wait)
  99. {
  100. struct usb_mixer_interface *mixer = hw->private_data;
  101. poll_wait(file, &mixer->rc_waitq, wait);
  102. return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
  103. }
  104. static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
  105. {
  106. struct snd_hwdep *hwdep;
  107. int err, len, i;
  108. for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
  109. if (rc_configs[i].usb_id == mixer->chip->usb_id)
  110. break;
  111. if (i >= ARRAY_SIZE(rc_configs))
  112. return 0;
  113. mixer->rc_cfg = &rc_configs[i];
  114. len = mixer->rc_cfg->packet_length;
  115. init_waitqueue_head(&mixer->rc_waitq);
  116. err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
  117. if (err < 0)
  118. return err;
  119. snprintf(hwdep->name, sizeof(hwdep->name),
  120. "%s remote control", mixer->chip->card->shortname);
  121. hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
  122. hwdep->private_data = mixer;
  123. hwdep->ops.read = snd_usb_sbrc_hwdep_read;
  124. hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
  125. hwdep->exclusive = 1;
  126. mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
  127. if (!mixer->rc_urb)
  128. return -ENOMEM;
  129. mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
  130. if (!mixer->rc_setup_packet) {
  131. usb_free_urb(mixer->rc_urb);
  132. mixer->rc_urb = NULL;
  133. return -ENOMEM;
  134. }
  135. mixer->rc_setup_packet->bRequestType =
  136. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  137. mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
  138. mixer->rc_setup_packet->wValue = cpu_to_le16(0);
  139. mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
  140. mixer->rc_setup_packet->wLength = cpu_to_le16(len);
  141. usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
  142. usb_rcvctrlpipe(mixer->chip->dev, 0),
  143. (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
  144. snd_usb_soundblaster_remote_complete, mixer);
  145. return 0;
  146. }
  147. #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
  148. static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  149. {
  150. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  151. int index = kcontrol->private_value;
  152. ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
  153. return 0;
  154. }
  155. static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  156. {
  157. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  158. int index = kcontrol->private_value;
  159. int value = ucontrol->value.integer.value[0];
  160. int err, changed;
  161. if (value > 1)
  162. return -EINVAL;
  163. changed = value != mixer->audigy2nx_leds[index];
  164. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042))
  165. err = snd_usb_ctl_msg(mixer->chip->dev,
  166. usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
  167. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  168. !value, 0, NULL, 0, 100);
  169. else
  170. err = snd_usb_ctl_msg(mixer->chip->dev,
  171. usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
  172. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  173. value, index + 2, NULL, 0, 100);
  174. if (err < 0)
  175. return err;
  176. mixer->audigy2nx_leds[index] = value;
  177. return changed;
  178. }
  179. static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
  180. {
  181. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  182. .name = "CMSS LED Switch",
  183. .info = snd_audigy2nx_led_info,
  184. .get = snd_audigy2nx_led_get,
  185. .put = snd_audigy2nx_led_put,
  186. .private_value = 0,
  187. },
  188. {
  189. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  190. .name = "Power LED Switch",
  191. .info = snd_audigy2nx_led_info,
  192. .get = snd_audigy2nx_led_get,
  193. .put = snd_audigy2nx_led_put,
  194. .private_value = 1,
  195. },
  196. {
  197. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  198. .name = "Dolby Digital LED Switch",
  199. .info = snd_audigy2nx_led_info,
  200. .get = snd_audigy2nx_led_get,
  201. .put = snd_audigy2nx_led_put,
  202. .private_value = 2,
  203. },
  204. };
  205. static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
  206. {
  207. int i, err;
  208. for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
  209. /* USB X-Fi S51 doesn't have a CMSS LED */
  210. if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
  211. continue;
  212. if (i > 1 && /* Live24ext has 2 LEDs only */
  213. (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  214. mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
  215. mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
  216. break;
  217. err = snd_ctl_add(mixer->chip->card,
  218. snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
  219. if (err < 0)
  220. return err;
  221. }
  222. mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
  223. return 0;
  224. }
  225. static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
  226. struct snd_info_buffer *buffer)
  227. {
  228. static const struct sb_jack {
  229. int unitid;
  230. const char *name;
  231. } jacks_audigy2nx[] = {
  232. {4, "dig in "},
  233. {7, "line in"},
  234. {19, "spk out"},
  235. {20, "hph out"},
  236. {-1, NULL}
  237. }, jacks_live24ext[] = {
  238. {4, "line in"}, /* &1=Line, &2=Mic*/
  239. {3, "hph out"}, /* headphones */
  240. {0, "RC "}, /* last command, 6 bytes see rc_config above */
  241. {-1, NULL}
  242. };
  243. const struct sb_jack *jacks;
  244. struct usb_mixer_interface *mixer = entry->private_data;
  245. int i, err;
  246. u8 buf[3];
  247. snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
  248. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
  249. jacks = jacks_audigy2nx;
  250. else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  251. mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
  252. jacks = jacks_live24ext;
  253. else
  254. return;
  255. for (i = 0; jacks[i].name; ++i) {
  256. snd_iprintf(buffer, "%s: ", jacks[i].name);
  257. err = snd_usb_ctl_msg(mixer->chip->dev,
  258. usb_rcvctrlpipe(mixer->chip->dev, 0),
  259. UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
  260. USB_RECIP_INTERFACE, 0,
  261. jacks[i].unitid << 8, buf, 3, 100);
  262. if (err == 3 && (buf[0] == 3 || buf[0] == 6))
  263. snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
  264. else
  265. snd_iprintf(buffer, "?\n");
  266. }
  267. }
  268. static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
  269. struct snd_ctl_elem_value *ucontrol)
  270. {
  271. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  272. ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
  273. return 0;
  274. }
  275. static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
  276. struct snd_ctl_elem_value *ucontrol)
  277. {
  278. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  279. u8 old_status, new_status;
  280. int err, changed;
  281. old_status = mixer->xonar_u1_status;
  282. if (ucontrol->value.integer.value[0])
  283. new_status = old_status | 0x02;
  284. else
  285. new_status = old_status & ~0x02;
  286. changed = new_status != old_status;
  287. err = snd_usb_ctl_msg(mixer->chip->dev,
  288. usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
  289. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  290. 50, 0, &new_status, 1, 100);
  291. if (err < 0)
  292. return err;
  293. mixer->xonar_u1_status = new_status;
  294. return changed;
  295. }
  296. static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
  297. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  298. .name = "Digital Playback Switch",
  299. .info = snd_ctl_boolean_mono_info,
  300. .get = snd_xonar_u1_switch_get,
  301. .put = snd_xonar_u1_switch_put,
  302. };
  303. static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
  304. {
  305. int err;
  306. err = snd_ctl_add(mixer->chip->card,
  307. snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
  308. if (err < 0)
  309. return err;
  310. mixer->xonar_u1_status = 0x05;
  311. return 0;
  312. }
  313. void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
  314. unsigned char samplerate_id)
  315. {
  316. struct usb_mixer_interface *mixer;
  317. struct usb_mixer_elem_info *cval;
  318. int unitid = 12; /* SamleRate ExtensionUnit ID */
  319. list_for_each_entry(mixer, &chip->mixer_list, list) {
  320. cval = mixer->id_elems[unitid];
  321. if (cval) {
  322. snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
  323. cval->control << 8,
  324. samplerate_id);
  325. snd_usb_mixer_notify_id(mixer, unitid);
  326. }
  327. break;
  328. }
  329. }
  330. int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
  331. {
  332. int err;
  333. struct snd_info_entry *entry;
  334. if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
  335. return err;
  336. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
  337. mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  338. mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
  339. mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
  340. if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
  341. return err;
  342. if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
  343. snd_info_set_text_ops(entry, mixer,
  344. snd_audigy2nx_proc_read);
  345. }
  346. if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
  347. mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
  348. err = snd_xonar_u1_controls_create(mixer);
  349. if (err < 0)
  350. return err;
  351. }
  352. return 0;
  353. }
  354. void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
  355. int unitid)
  356. {
  357. if (!mixer->rc_cfg)
  358. return;
  359. /* unit ids specific to Extigy/Audigy 2 NX: */
  360. switch (unitid) {
  361. case 0: /* remote control */
  362. mixer->rc_urb->dev = mixer->chip->dev;
  363. usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
  364. break;
  365. case 4: /* digital in jack */
  366. case 7: /* line in jacks */
  367. case 19: /* speaker out jacks */
  368. case 20: /* headphones out jack */
  369. break;
  370. /* live24ext: 4 = line-in jack */
  371. case 3: /* hp-out jack (may actuate Mute) */
  372. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  373. mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
  374. snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
  375. break;
  376. default:
  377. snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
  378. break;
  379. }
  380. }