helper.c 3.7 KB

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