File Index
Files and directories
Sign in
FILE

helper.c

File preview with syntax highlighting.

linux-6.19.14/sound/usb/helper.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 */
  4
  5#include <linux/init.h>
  6#include <linux/slab.h>
  7#include <linux/usb.h>
  8
  9#include "usbaudio.h"
 10#include "helper.h"
 11#include "quirks.h"
 12
 13/*
 14 * combine bytes and get an integer value
 15 */
 16unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
 17{
 18	switch (size) {
 19	case 1:  return *bytes;
 20	case 2:  return combine_word(bytes);
 21	case 3:  return combine_triple(bytes);
 22	case 4:  return combine_quad(bytes);
 23	default: return 0;
 24	}
 25}
 26
 27/*
 28 * parse descriptor buffer and return the pointer starting the given
 29 * descriptor type.
 30 */
 31void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
 32{
 33	u8 *p, *end, *next;
 34
 35	p = descstart;
 36	end = p + desclen;
 37	for (; p < end;) {
 38		if (p[0] < 2)
 39			return NULL;
 40		next = p + p[0];
 41		if (next > end)
 42			return NULL;
 43		if (p[1] == dtype && (!after || (void *)p > after)) {
 44			return p;
 45		}
 46		p = next;
 47	}
 48	return NULL;
 49}
 50
 51/*
 52 * find a class-specified interface descriptor with the given subtype.
 53 */
 54void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
 55{
 56	unsigned char *p = after;
 57
 58	while ((p = snd_usb_find_desc(buffer, buflen, p,
 59				      USB_DT_CS_INTERFACE)) != NULL) {
 60		if (p[0] >= 3 && p[2] == dsubtype)
 61			return p;
 62	}
 63	return NULL;
 64}
 65EXPORT_SYMBOL_GPL(snd_usb_find_csint_desc);
 66
 67/*
 68 * Wrapper for usb_control_msg().
 69 * Allocates a temp buffer to prevent dmaing from/to the stack.
 70 */
 71int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
 72		    __u8 requesttype, __u16 value, __u16 index, void *data,
 73		    __u16 size)
 74{
 75	int err;
 76	void *buf = NULL;
 77	int timeout;
 78
 79	if (usb_pipe_type_check(dev, pipe))
 80		return -EINVAL;
 81
 82	if (size > 0) {
 83		buf = kmemdup(data, size, GFP_KERNEL);
 84		if (!buf)
 85			return -ENOMEM;
 86	}
 87
 88	if (requesttype & USB_DIR_IN)
 89		timeout = USB_CTRL_GET_TIMEOUT;
 90	else
 91		timeout = USB_CTRL_SET_TIMEOUT;
 92
 93	err = usb_control_msg(dev, pipe, request, requesttype,
 94			      value, index, buf, size, timeout);
 95
 96	if (size > 0) {
 97		memcpy(data, buf, size);
 98		kfree(buf);
 99	}
100
101	snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype,
102			      value, index, data, size);
103
104	return err;
105}
106
107unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
108					 struct usb_host_interface *alts)
109{
110	switch (snd_usb_get_speed(chip->dev)) {
111	case USB_SPEED_HIGH:
112	case USB_SPEED_SUPER:
113	case USB_SPEED_SUPER_PLUS:
114		if (get_endpoint(alts, 0)->bInterval >= 1 &&
115		    get_endpoint(alts, 0)->bInterval <= 4)
116			return get_endpoint(alts, 0)->bInterval - 1;
117		break;
118	default:
119		break;
120	}
121	return 0;
122}
123
124struct usb_host_interface *
125snd_usb_get_host_interface(struct snd_usb_audio *chip, int ifnum, int altsetting)
126{
127	struct usb_interface *iface;
128
129	iface = usb_ifnum_to_if(chip->dev, ifnum);
130	if (!iface)
131		return NULL;
132	return usb_altnum_to_altsetting(iface, altsetting);
133}
134
135int snd_usb_add_ctrl_interface_link(struct snd_usb_audio *chip, int ifnum,
136		int ctrlif)
137{
138	struct usb_device *dev = chip->dev;
139	struct usb_host_interface *host_iface;
140
141	if (chip->num_intf_to_ctrl >= MAX_CARD_INTERFACES) {
142		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
143		return -EINVAL;
144	}
145
146	/* find audiocontrol interface */
147	host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
148
149	chip->intf_to_ctrl[chip->num_intf_to_ctrl].interface = ifnum;
150	chip->intf_to_ctrl[chip->num_intf_to_ctrl].ctrl_intf = host_iface;
151	chip->num_intf_to_ctrl++;
152
153	return 0;
154}
155
156struct usb_host_interface *snd_usb_find_ctrl_interface(struct snd_usb_audio *chip,
157							int ifnum)
158{
159	int i;
160
161	for (i = 0; i < chip->num_intf_to_ctrl; ++i)
162		if (chip->intf_to_ctrl[i].interface == ifnum)
163			return chip->intf_to_ctrl[i].ctrl_intf;
164
165	/* Fallback to first audiocontrol interface */
166	return chip->ctrl_intf;
167}
~/ Read-only file preview.