Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54a8c500 authored by Daniel Mack's avatar Daniel Mack Committed by Takashi Iwai
Browse files

ALSA: usb-audio: add support for Native Instruments MK2 devices



The MK2 generation of Native Instruments' sound cards are in fact
compliant to the USB audio standard of version 2 and other approved USB
standards. However, they come up as vendor-specific device when first
connected but can be told to come up with a new set of descriptors
upon their next enumeration. The interfaces announced by the new
descriptors will be handled by the kernel's class drivers. This is done
by issuing a vendor specific device request and sending the device to
reset.

There are also some vendor-specific USB requests for some mixer elements
that can't be exported in a standard compliant way. The driver now
supports them with quirks handling mechanisms.

Signed-off-by: default avatarDaniel Mack <daniel@caiaq.de>
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent df8d81a3
Loading
Loading
Loading
Loading
+153 −0
Original line number Diff line number Diff line
@@ -346,6 +346,141 @@ static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
	return 0;
}

/* Native Instruments device quirks */

#define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))

static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
	struct usb_device *dev = mixer->chip->dev;
	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
	u16 wIndex = kcontrol->private_value & 0xffff;
	u8 tmp;

	int ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), bRequest,
				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
				  0, cpu_to_le16(wIndex),
				  &tmp, sizeof(tmp), 1000);

	if (ret < 0) {
		snd_printk(KERN_ERR
			   "unable to issue vendor read request (ret = %d)", ret);
		return ret;
	}

	ucontrol->value.integer.value[0] = tmp;

	return 0;
}

static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
	struct usb_device *dev = mixer->chip->dev;
	u8 bRequest = (kcontrol->private_value >> 16) & 0xff;
	u16 wIndex = kcontrol->private_value & 0xffff;
	u16 wValue = ucontrol->value.integer.value[0];

	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), bRequest,
				  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
				  cpu_to_le16(wValue), cpu_to_le16(wIndex),
				  NULL, 0, 1000);

	if (ret < 0) {
		snd_printk(KERN_ERR
			   "unable to issue vendor write request (ret = %d)", ret);
		return ret;
	}

	return 0;
}

static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
	{
		.name = "Direct Thru Channel A",
		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
	},
	{
		.name = "Direct Thru Channel B",
		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
	},
	{
		.name = "Phono Input Channel A",
		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
	},
	{
		.name = "Phono Input Channel B",
		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
	},
};

static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
	{
		.name = "Direct Thru Channel A",
		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
	},
	{
		.name = "Direct Thru Channel B",
		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
	},
	{
		.name = "Direct Thru Channel C",
		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
	},
	{
		.name = "Direct Thru Channel D",
		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
	},
	{
		.name = "Phono Input Channel A",
		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
	},
	{
		.name = "Phono Input Channel B",
		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
	},
	{
		.name = "Phono Input Channel C",
		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
	},
	{
		.name = "Phono Input Channel D",
		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
	},
};

static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
					      const struct snd_kcontrol_new *kc,
					      unsigned int count)
{
	int i, err = 0;
	struct snd_kcontrol_new template = {
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
		.get = snd_nativeinstruments_control_get,
		.put = snd_nativeinstruments_control_put,
		.info = snd_ctl_boolean_mono_info,
	};

	for (i = 0; i < count; i++) {
		struct snd_kcontrol *c;

		template.name = kc[i].name;
		template.private_value = kc[i].private_value;

		c = snd_ctl_new1(&template, mixer);
		err = snd_ctl_add(mixer->chip->card, c);

		if (err < 0)
			break;
	}

	return err;
}

void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
			       unsigned char samplerate_id)
{
@@ -391,6 +526,24 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
			return err;
	}

	/* Traktor Audio 6 */
	if (mixer->chip->usb_id == USB_ID(0x17cc, 0x1011)) {
		err = snd_nativeinstruments_create_mixer(mixer,
				snd_nativeinstruments_ta6_mixers,
				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
		if (err < 0)
			return err;
	}

	/* Traktor Audio 10 */
	if (mixer->chip->usb_id == USB_ID(0x17cc, 0x1021)) {
		err = snd_nativeinstruments_create_mixer(mixer,
				snd_nativeinstruments_ta10_mixers,
				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
		if (err < 0)
			return err;
	}

	return 0;
}

+14 −0
Original line number Diff line number Diff line
@@ -2283,6 +2283,20 @@ YAMAHA_DEVICE(0x7010, "UB99"),
	}
},

/* Native Instruments MK2 series */
{
	/* Traktor Audio 6 */
	.match_flags = USB_DEVICE_ID_MATCH_DEVICE,
	.idVendor = 0x17cc,
	.idProduct = 0x1010,
},
{
	/* Traktor Audio 10 */
	.match_flags = USB_DEVICE_ID_MATCH_DEVICE,
	.idVendor = 0x17cc,
	.idProduct = 0x1020,
},

/* Miditech devices */
{
	USB_DEVICE(0x4752, 0x0011),
+33 −0
Original line number Diff line number Diff line
@@ -424,6 +424,34 @@ static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
	return 0;
}

/*
 * Some sound cards from Native Instruments are in fact compliant to the USB
 * audio standard of version 2 and other approved USB standards, even though
 * they come up as vendor-specific device when first connected.
 *
 * However, they can be told to come up with a new set of descriptors
 * upon their next enumeration, and the interfaces announced by the new
 * descriptors will then be handled by the kernel's class drivers. As the
 * product ID will also change, no further checks are required.
 */

static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
{
	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
				  0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
				  cpu_to_le16(1), 0, NULL, 0, 1000);

	if (ret < 0)
		return ret;

	usb_reset_device(dev);

	/* return -EAGAIN, so the creation of an audio interface for this
	 * temporary device is aborted. The device will reconnect with a
	 * new product ID */
	return -EAGAIN;
}

/*
 * Setup quirks
 */
@@ -510,6 +538,11 @@ int snd_usb_apply_boot_quirk(struct usb_device *dev,
	if (id == USB_ID(0x133e, 0x0815))
		return snd_usb_accessmusic_boot_quirk(dev);

	/* Native Instruments Devices */
	if (id == USB_ID(0x17cc, 0x1010) || /* Traktor Audio 6 */
		id == USB_ID(0x17cc, 0x1020)) /* Traktor Audio 10 */
		return snd_usb_nativeinstruments_boot_quirk(dev);

	return 0;
}