helper.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include "usbaudio.h"
  21. #include "helper.h"
  22. #include "quirks.h"
  23. /*
  24. * combine bytes and get an integer value
  25. */
  26. unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
  27. {
  28. switch (size) {
  29. case 1: return *bytes;
  30. case 2: return combine_word(bytes);
  31. case 3: return combine_triple(bytes);
  32. case 4: return combine_quad(bytes);
  33. default: return 0;
  34. }
  35. }
  36. /*
  37. * parse descriptor buffer and return the pointer starting the given
  38. * descriptor type.
  39. */
  40. void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
  41. {
  42. u8 *p, *end, *next;
  43. p = descstart;
  44. end = p + desclen;
  45. for (; p < end;) {
  46. if (p[0] < 2)
  47. return NULL;
  48. next = p + p[0];
  49. if (next > end)
  50. return NULL;
  51. if (p[1] == dtype && (!after || (void *)p > after)) {
  52. return p;
  53. }
  54. p = next;
  55. }
  56. return NULL;
  57. }
  58. /*
  59. * find a class-specified interface descriptor with the given subtype.
  60. */
  61. void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
  62. {
  63. unsigned char *p = after;
  64. while ((p = snd_usb_find_desc(buffer, buflen, p,
  65. USB_DT_CS_INTERFACE)) != NULL) {
  66. if (p[0] >= 3 && p[2] == dsubtype)
  67. return p;
  68. }
  69. return NULL;
  70. }
  71. /*
  72. * Wrapper for usb_control_msg().
  73. * Allocates a temp buffer to prevent dmaing from/to the stack.
  74. */
  75. int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  76. __u8 requesttype, __u16 value, __u16 index, void *data,
  77. __u16 size)
  78. {
  79. int err;
  80. void *buf = NULL;
  81. int timeout;
  82. if (size > 0) {
  83. buf = kmemdup(data, size, GFP_KERNEL);
  84. if (!buf)
  85. return -ENOMEM;
  86. }
  87. if (requesttype & USB_DIR_IN)
  88. timeout = USB_CTRL_GET_TIMEOUT;
  89. else
  90. timeout = USB_CTRL_SET_TIMEOUT;
  91. err = usb_control_msg(dev, pipe, request, requesttype,
  92. value, index, buf, size, timeout);
  93. if (size > 0) {
  94. memcpy(data, buf, size);
  95. kfree(buf);
  96. }
  97. snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype,
  98. value, index, data, size);
  99. return err;
  100. }
  101. unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
  102. struct usb_host_interface *alts)
  103. {
  104. switch (snd_usb_get_speed(chip->dev)) {
  105. case USB_SPEED_HIGH:
  106. case USB_SPEED_WIRELESS:
  107. case USB_SPEED_SUPER:
  108. if (get_endpoint(alts, 0)->bInterval >= 1 &&
  109. get_endpoint(alts, 0)->bInterval <= 4)
  110. return get_endpoint(alts, 0)->bInterval - 1;
  111. break;
  112. default:
  113. break;
  114. }
  115. return 0;
  116. }