helper.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /*
  59. * Wrapper for usb_control_msg().
  60. * Allocates a temp buffer to prevent dmaing from/to the stack.
  61. */
  62. int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  63. __u8 requesttype, __u16 value, __u16 index, void *data,
  64. __u16 size)
  65. {
  66. int err;
  67. void *buf = NULL;
  68. int timeout;
  69. if (usb_pipe_type_check(dev, pipe))
  70. return -EINVAL;
  71. if (size > 0) {
  72. buf = kmemdup(data, size, GFP_KERNEL);
  73. if (!buf)
  74. return -ENOMEM;
  75. }
  76. if (requesttype & USB_DIR_IN)
  77. timeout = USB_CTRL_GET_TIMEOUT;
  78. else
  79. timeout = USB_CTRL_SET_TIMEOUT;
  80. err = usb_control_msg(dev, pipe, request, requesttype,
  81. value, index, buf, size, timeout);
  82. if (size > 0) {
  83. memcpy(data, buf, size);
  84. kfree(buf);
  85. }
  86. snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype,
  87. value, index, data, size);
  88. return err;
  89. }
  90. unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
  91. struct usb_host_interface *alts)
  92. {
  93. switch (snd_usb_get_speed(chip->dev)) {
  94. case USB_SPEED_HIGH:
  95. case USB_SPEED_WIRELESS:
  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. }