mixer_quirks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
  60. };
  61. static void snd_usb_soundblaster_remote_complete(struct urb *urb)
  62. {
  63. struct usb_mixer_interface *mixer = urb->context;
  64. const struct rc_config *rc = mixer->rc_cfg;
  65. u32 code;
  66. if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
  67. return;
  68. code = mixer->rc_buffer[rc->offset];
  69. if (rc->length == 2)
  70. code |= mixer->rc_buffer[rc->offset + 1] << 8;
  71. /* the Mute button actually changes the mixer control */
  72. if (code == rc->mute_code)
  73. snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
  74. mixer->rc_code = code;
  75. wmb();
  76. wake_up(&mixer->rc_waitq);
  77. }
  78. static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
  79. long count, loff_t *offset)
  80. {
  81. struct usb_mixer_interface *mixer = hw->private_data;
  82. int err;
  83. u32 rc_code;
  84. if (count != 1 && count != 4)
  85. return -EINVAL;
  86. err = wait_event_interruptible(mixer->rc_waitq,
  87. (rc_code = xchg(&mixer->rc_code, 0)) != 0);
  88. if (err == 0) {
  89. if (count == 1)
  90. err = put_user(rc_code, buf);
  91. else
  92. err = put_user(rc_code, (u32 __user *)buf);
  93. }
  94. return err < 0 ? err : count;
  95. }
  96. static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
  97. poll_table *wait)
  98. {
  99. struct usb_mixer_interface *mixer = hw->private_data;
  100. poll_wait(file, &mixer->rc_waitq, wait);
  101. return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
  102. }
  103. static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
  104. {
  105. struct snd_hwdep *hwdep;
  106. int err, len, i;
  107. for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
  108. if (rc_configs[i].usb_id == mixer->chip->usb_id)
  109. break;
  110. if (i >= ARRAY_SIZE(rc_configs))
  111. return 0;
  112. mixer->rc_cfg = &rc_configs[i];
  113. len = mixer->rc_cfg->packet_length;
  114. init_waitqueue_head(&mixer->rc_waitq);
  115. err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
  116. if (err < 0)
  117. return err;
  118. snprintf(hwdep->name, sizeof(hwdep->name),
  119. "%s remote control", mixer->chip->card->shortname);
  120. hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
  121. hwdep->private_data = mixer;
  122. hwdep->ops.read = snd_usb_sbrc_hwdep_read;
  123. hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
  124. hwdep->exclusive = 1;
  125. mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
  126. if (!mixer->rc_urb)
  127. return -ENOMEM;
  128. mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
  129. if (!mixer->rc_setup_packet) {
  130. usb_free_urb(mixer->rc_urb);
  131. mixer->rc_urb = NULL;
  132. return -ENOMEM;
  133. }
  134. mixer->rc_setup_packet->bRequestType =
  135. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  136. mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
  137. mixer->rc_setup_packet->wValue = cpu_to_le16(0);
  138. mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
  139. mixer->rc_setup_packet->wLength = cpu_to_le16(len);
  140. usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
  141. usb_rcvctrlpipe(mixer->chip->dev, 0),
  142. (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
  143. snd_usb_soundblaster_remote_complete, mixer);
  144. return 0;
  145. }
  146. #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
  147. static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  148. {
  149. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  150. int index = kcontrol->private_value;
  151. ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
  152. return 0;
  153. }
  154. static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  155. {
  156. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  157. int index = kcontrol->private_value;
  158. int value = ucontrol->value.integer.value[0];
  159. int err, changed;
  160. if (value > 1)
  161. return -EINVAL;
  162. changed = value != mixer->audigy2nx_leds[index];
  163. err = snd_usb_ctl_msg(mixer->chip->dev,
  164. usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
  165. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  166. value, index + 2, NULL, 0, 100);
  167. if (err < 0)
  168. return err;
  169. mixer->audigy2nx_leds[index] = value;
  170. return changed;
  171. }
  172. static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
  173. {
  174. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  175. .name = "CMSS LED Switch",
  176. .info = snd_audigy2nx_led_info,
  177. .get = snd_audigy2nx_led_get,
  178. .put = snd_audigy2nx_led_put,
  179. .private_value = 0,
  180. },
  181. {
  182. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  183. .name = "Power LED Switch",
  184. .info = snd_audigy2nx_led_info,
  185. .get = snd_audigy2nx_led_get,
  186. .put = snd_audigy2nx_led_put,
  187. .private_value = 1,
  188. },
  189. {
  190. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  191. .name = "Dolby Digital LED Switch",
  192. .info = snd_audigy2nx_led_info,
  193. .get = snd_audigy2nx_led_get,
  194. .put = snd_audigy2nx_led_put,
  195. .private_value = 2,
  196. },
  197. };
  198. static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
  199. {
  200. int i, err;
  201. for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
  202. if (i > 1 && /* Live24ext has 2 LEDs only */
  203. (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  204. mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
  205. break;
  206. err = snd_ctl_add(mixer->chip->card,
  207. snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
  208. if (err < 0)
  209. return err;
  210. }
  211. mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
  212. return 0;
  213. }
  214. static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
  215. struct snd_info_buffer *buffer)
  216. {
  217. static const struct sb_jack {
  218. int unitid;
  219. const char *name;
  220. } jacks_audigy2nx[] = {
  221. {4, "dig in "},
  222. {7, "line in"},
  223. {19, "spk out"},
  224. {20, "hph out"},
  225. {-1, NULL}
  226. }, jacks_live24ext[] = {
  227. {4, "line in"}, /* &1=Line, &2=Mic*/
  228. {3, "hph out"}, /* headphones */
  229. {0, "RC "}, /* last command, 6 bytes see rc_config above */
  230. {-1, NULL}
  231. };
  232. const struct sb_jack *jacks;
  233. struct usb_mixer_interface *mixer = entry->private_data;
  234. int i, err;
  235. u8 buf[3];
  236. snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
  237. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
  238. jacks = jacks_audigy2nx;
  239. else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  240. mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
  241. jacks = jacks_live24ext;
  242. else
  243. return;
  244. for (i = 0; jacks[i].name; ++i) {
  245. snd_iprintf(buffer, "%s: ", jacks[i].name);
  246. err = snd_usb_ctl_msg(mixer->chip->dev,
  247. usb_rcvctrlpipe(mixer->chip->dev, 0),
  248. UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
  249. USB_RECIP_INTERFACE, 0,
  250. jacks[i].unitid << 8, buf, 3, 100);
  251. if (err == 3 && (buf[0] == 3 || buf[0] == 6))
  252. snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
  253. else
  254. snd_iprintf(buffer, "?\n");
  255. }
  256. }
  257. static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
  258. struct snd_ctl_elem_value *ucontrol)
  259. {
  260. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  261. ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
  262. return 0;
  263. }
  264. static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  268. u8 old_status, new_status;
  269. int err, changed;
  270. old_status = mixer->xonar_u1_status;
  271. if (ucontrol->value.integer.value[0])
  272. new_status = old_status | 0x02;
  273. else
  274. new_status = old_status & ~0x02;
  275. changed = new_status != old_status;
  276. err = snd_usb_ctl_msg(mixer->chip->dev,
  277. usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
  278. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  279. 50, 0, &new_status, 1, 100);
  280. if (err < 0)
  281. return err;
  282. mixer->xonar_u1_status = new_status;
  283. return changed;
  284. }
  285. static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
  286. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  287. .name = "Digital Playback Switch",
  288. .info = snd_ctl_boolean_mono_info,
  289. .get = snd_xonar_u1_switch_get,
  290. .put = snd_xonar_u1_switch_put,
  291. };
  292. static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
  293. {
  294. int err;
  295. err = snd_ctl_add(mixer->chip->card,
  296. snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
  297. if (err < 0)
  298. return err;
  299. mixer->xonar_u1_status = 0x05;
  300. return 0;
  301. }
  302. void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
  303. unsigned char samplerate_id)
  304. {
  305. struct usb_mixer_interface *mixer;
  306. struct usb_mixer_elem_info *cval;
  307. int unitid = 12; /* SamleRate ExtensionUnit ID */
  308. list_for_each_entry(mixer, &chip->mixer_list, list) {
  309. cval = mixer->id_elems[unitid];
  310. if (cval) {
  311. snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
  312. cval->control << 8,
  313. samplerate_id);
  314. snd_usb_mixer_notify_id(mixer, unitid);
  315. }
  316. break;
  317. }
  318. }
  319. int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
  320. {
  321. int err;
  322. struct snd_info_entry *entry;
  323. if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
  324. return err;
  325. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
  326. mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  327. mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
  328. if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
  329. return err;
  330. if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
  331. snd_info_set_text_ops(entry, mixer,
  332. snd_audigy2nx_proc_read);
  333. }
  334. if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
  335. mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
  336. err = snd_xonar_u1_controls_create(mixer);
  337. if (err < 0)
  338. return err;
  339. }
  340. return 0;
  341. }
  342. void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
  343. int unitid)
  344. {
  345. if (!mixer->rc_cfg)
  346. return;
  347. /* unit ids specific to Extigy/Audigy 2 NX: */
  348. switch (unitid) {
  349. case 0: /* remote control */
  350. mixer->rc_urb->dev = mixer->chip->dev;
  351. usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
  352. break;
  353. case 4: /* digital in jack */
  354. case 7: /* line in jacks */
  355. case 19: /* speaker out jacks */
  356. case 20: /* headphones out jack */
  357. break;
  358. /* live24ext: 4 = line-in jack */
  359. case 3: /* hp-out jack (may actuate Mute) */
  360. if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
  361. mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
  362. snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
  363. break;
  364. default:
  365. snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
  366. break;
  367. }
  368. }