Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  */
0004 
0005 #include <linux/init.h>
0006 #include <linux/slab.h>
0007 #include <linux/usb.h>
0008 
0009 #include "usbaudio.h"
0010 #include "helper.h"
0011 #include "quirks.h"
0012 
0013 /*
0014  * combine bytes and get an integer value
0015  */
0016 unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
0017 {
0018     switch (size) {
0019     case 1:  return *bytes;
0020     case 2:  return combine_word(bytes);
0021     case 3:  return combine_triple(bytes);
0022     case 4:  return combine_quad(bytes);
0023     default: return 0;
0024     }
0025 }
0026 
0027 /*
0028  * parse descriptor buffer and return the pointer starting the given
0029  * descriptor type.
0030  */
0031 void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
0032 {
0033     u8 *p, *end, *next;
0034 
0035     p = descstart;
0036     end = p + desclen;
0037     for (; p < end;) {
0038         if (p[0] < 2)
0039             return NULL;
0040         next = p + p[0];
0041         if (next > end)
0042             return NULL;
0043         if (p[1] == dtype && (!after || (void *)p > after)) {
0044             return p;
0045         }
0046         p = next;
0047     }
0048     return NULL;
0049 }
0050 
0051 /*
0052  * find a class-specified interface descriptor with the given subtype.
0053  */
0054 void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
0055 {
0056     unsigned char *p = after;
0057 
0058     while ((p = snd_usb_find_desc(buffer, buflen, p,
0059                       USB_DT_CS_INTERFACE)) != NULL) {
0060         if (p[0] >= 3 && p[2] == dsubtype)
0061             return p;
0062     }
0063     return NULL;
0064 }
0065 
0066 /*
0067  * Wrapper for usb_control_msg().
0068  * Allocates a temp buffer to prevent dmaing from/to the stack.
0069  */
0070 int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
0071             __u8 requesttype, __u16 value, __u16 index, void *data,
0072             __u16 size)
0073 {
0074     int err;
0075     void *buf = NULL;
0076     int timeout;
0077 
0078     if (usb_pipe_type_check(dev, pipe))
0079         return -EINVAL;
0080 
0081     if (size > 0) {
0082         buf = kmemdup(data, size, GFP_KERNEL);
0083         if (!buf)
0084             return -ENOMEM;
0085     }
0086 
0087     if (requesttype & USB_DIR_IN)
0088         timeout = USB_CTRL_GET_TIMEOUT;
0089     else
0090         timeout = USB_CTRL_SET_TIMEOUT;
0091 
0092     err = usb_control_msg(dev, pipe, request, requesttype,
0093                   value, index, buf, size, timeout);
0094 
0095     if (size > 0) {
0096         memcpy(data, buf, size);
0097         kfree(buf);
0098     }
0099 
0100     snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype,
0101                   value, index, data, size);
0102 
0103     return err;
0104 }
0105 
0106 unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
0107                      struct usb_host_interface *alts)
0108 {
0109     switch (snd_usb_get_speed(chip->dev)) {
0110     case USB_SPEED_HIGH:
0111     case USB_SPEED_WIRELESS:
0112     case USB_SPEED_SUPER:
0113     case USB_SPEED_SUPER_PLUS:
0114         if (get_endpoint(alts, 0)->bInterval >= 1 &&
0115             get_endpoint(alts, 0)->bInterval <= 4)
0116             return get_endpoint(alts, 0)->bInterval - 1;
0117         break;
0118     default:
0119         break;
0120     }
0121     return 0;
0122 }
0123 
0124 struct usb_host_interface *
0125 snd_usb_get_host_interface(struct snd_usb_audio *chip, int ifnum, int altsetting)
0126 {
0127     struct usb_interface *iface;
0128 
0129     iface = usb_ifnum_to_if(chip->dev, ifnum);
0130     if (!iface)
0131         return NULL;
0132     return usb_altnum_to_altsetting(iface, altsetting);
0133 }