helper.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /*
  23. * combine bytes and get an integer value
  24. */
  25. unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
  26. {
  27. switch (size) {
  28. case 1: return *bytes;
  29. case 2: return combine_word(bytes);
  30. case 3: return combine_triple(bytes);
  31. case 4: return combine_quad(bytes);
  32. default: return 0;
  33. }
  34. }
  35. /*
  36. * parse descriptor buffer and return the pointer starting the given
  37. * descriptor type.
  38. */
  39. void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
  40. {
  41. u8 *p, *end, *next;
  42. p = descstart;
  43. end = p + desclen;
  44. for (; p < end;) {
  45. if (p[0] < 2)
  46. return NULL;
  47. next = p + p[0];
  48. if (next > end)
  49. return NULL;
  50. if (p[1] == dtype && (!after || (void *)p > after)) {
  51. return p;
  52. }
  53. p = next;
  54. }
  55. return NULL;
  56. }
  57. /*
  58. * find a class-specified interface descriptor with the given subtype.
  59. */
  60. void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
  61. {
  62. unsigned char *p = after;
  63. while ((p = snd_usb_find_desc(buffer, buflen, p,
  64. USB_DT_CS_INTERFACE)) != NULL) {
  65. if (p[0] >= 3 && p[2] == dsubtype)
  66. return p;
  67. }
  68. return NULL;
  69. }
  70. /*
  71. * Wrapper for usb_control_msg().
  72. * Allocates a temp buffer to prevent dmaing from/to the stack.
  73. */
  74. int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  75. __u8 requesttype, __u16 value, __u16 index, void *data,
  76. __u16 size, int timeout)
  77. {
  78. int err;
  79. void *buf = NULL;
  80. if (size > 0) {
  81. buf = kmemdup(data, size, GFP_KERNEL);
  82. if (!buf)
  83. return -ENOMEM;
  84. }
  85. err = usb_control_msg(dev, pipe, request, requesttype,
  86. value, index, buf, size, timeout);
  87. if (size > 0) {
  88. memcpy(data, buf, size);
  89. kfree(buf);
  90. }
  91. return err;
  92. }
  93. unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
  94. struct usb_host_interface *alts)
  95. {
  96. switch (snd_usb_get_speed(chip->dev)) {
  97. case USB_SPEED_HIGH:
  98. case USB_SPEED_SUPER:
  99. if (get_endpoint(alts, 0)->bInterval >= 1 &&
  100. get_endpoint(alts, 0)->bInterval <= 4)
  101. return get_endpoint(alts, 0)->bInterval - 1;
  102. break;
  103. default:
  104. break;
  105. }
  106. return 0;
  107. }