usbaudio.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __USBAUDIO_H
  3. #define __USBAUDIO_H
  4. /*
  5. * (Tentative) USB Audio Driver for ALSA
  6. *
  7. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  8. */
  9. /* handling of USB vendor/product ID pairs as 32-bit numbers */
  10. #define USB_ID(vendor, product) (((unsigned int)(vendor) << 16) | (product))
  11. #define USB_ID_VENDOR(id) ((id) >> 16)
  12. #define USB_ID_PRODUCT(id) ((u16)(id))
  13. /*
  14. *
  15. */
  16. struct media_device;
  17. struct media_intf_devnode;
  18. #define MAX_CARD_INTERFACES 16
  19. /*
  20. * Structure holding assosiation between Audio Control Interface
  21. * and given Streaming or Midi Interface.
  22. */
  23. struct snd_intf_to_ctrl {
  24. u8 interface;
  25. struct usb_host_interface *ctrl_intf;
  26. };
  27. struct snd_usb_audio {
  28. int index;
  29. struct usb_device *dev;
  30. struct snd_card *card;
  31. struct usb_interface *intf[MAX_CARD_INTERFACES];
  32. u32 usb_id;
  33. uint16_t quirk_type;
  34. struct mutex mutex;
  35. unsigned int system_suspend;
  36. atomic_t active;
  37. atomic_t shutdown;
  38. atomic_t usage_count;
  39. wait_queue_head_t shutdown_wait;
  40. unsigned int quirk_flags;
  41. unsigned int need_delayed_register:1; /* warn for delayed registration */
  42. int num_interfaces;
  43. int last_iface;
  44. int num_suspended_intf;
  45. int sample_rate_read_error;
  46. int badd_profile; /* UAC3 BADD profile */
  47. struct list_head pcm_list; /* list of pcm streams */
  48. struct list_head ep_list; /* list of audio-related endpoints */
  49. struct list_head iface_ref_list; /* list of interface refcounts */
  50. struct list_head clock_ref_list; /* list of clock refcounts */
  51. int pcm_devs;
  52. unsigned int num_rawmidis; /* number of created rawmidi devices */
  53. struct list_head midi_list; /* list of midi interfaces */
  54. struct list_head midi_v2_list; /* list of MIDI 2 interfaces */
  55. struct list_head mixer_list; /* list of mixer interfaces */
  56. int setup; /* from the 'device_setup' module param */
  57. bool generic_implicit_fb; /* from the 'implicit_fb' module param */
  58. bool autoclock; /* from the 'autoclock' module param */
  59. bool lowlatency; /* from the 'lowlatency' module param */
  60. struct usb_host_interface *ctrl_intf; /* the audio control interface */
  61. struct media_device *media_dev;
  62. struct media_intf_devnode *ctl_intf_media_devnode;
  63. unsigned int num_intf_to_ctrl;
  64. struct snd_intf_to_ctrl intf_to_ctrl[MAX_CARD_INTERFACES];
  65. };
  66. #define USB_AUDIO_IFACE_UNUSED ((void *)-1L)
  67. #define usb_audio_err(chip, fmt, args...) \
  68. dev_err(&(chip)->dev->dev, fmt, ##args)
  69. #define usb_audio_err_ratelimited(chip, fmt, args...) \
  70. dev_err_ratelimited(&(chip)->dev->dev, fmt, ##args)
  71. #define usb_audio_warn(chip, fmt, args...) \
  72. dev_warn(&(chip)->dev->dev, fmt, ##args)
  73. #define usb_audio_info(chip, fmt, args...) \
  74. dev_info(&(chip)->dev->dev, fmt, ##args)
  75. #define usb_audio_dbg(chip, fmt, args...) \
  76. dev_dbg(&(chip)->dev->dev, fmt, ##args)
  77. /*
  78. * Information about devices with broken descriptors
  79. */
  80. /* special values for .ifnum */
  81. #define QUIRK_NODEV_INTERFACE -3 /* return -ENODEV */
  82. #define QUIRK_NO_INTERFACE -2
  83. #define QUIRK_ANY_INTERFACE -1
  84. enum quirk_type {
  85. QUIRK_IGNORE_INTERFACE,
  86. QUIRK_COMPOSITE,
  87. QUIRK_AUTODETECT,
  88. QUIRK_MIDI_STANDARD_INTERFACE,
  89. QUIRK_MIDI_FIXED_ENDPOINT,
  90. QUIRK_MIDI_YAMAHA,
  91. QUIRK_MIDI_ROLAND,
  92. QUIRK_MIDI_MIDIMAN,
  93. QUIRK_MIDI_NOVATION,
  94. QUIRK_MIDI_RAW_BYTES,
  95. QUIRK_MIDI_EMAGIC,
  96. QUIRK_MIDI_CME,
  97. QUIRK_MIDI_AKAI,
  98. QUIRK_MIDI_US122L,
  99. QUIRK_MIDI_FTDI,
  100. QUIRK_MIDI_CH345,
  101. QUIRK_AUDIO_STANDARD_INTERFACE,
  102. QUIRK_AUDIO_FIXED_ENDPOINT,
  103. QUIRK_AUDIO_EDIROL_UAXX,
  104. QUIRK_AUDIO_STANDARD_MIXER,
  105. QUIRK_TYPE_COUNT
  106. };
  107. struct snd_usb_audio_quirk {
  108. const char *vendor_name;
  109. const char *product_name;
  110. int16_t ifnum;
  111. uint16_t type;
  112. const void *data;
  113. };
  114. #define combine_word(s) ((*(s)) | ((unsigned int)(s)[1] << 8))
  115. #define combine_triple(s) (combine_word(s) | ((unsigned int)(s)[2] << 16))
  116. #define combine_quad(s) (combine_triple(s) | ((unsigned int)(s)[3] << 24))
  117. int snd_usb_lock_shutdown(struct snd_usb_audio *chip);
  118. void snd_usb_unlock_shutdown(struct snd_usb_audio *chip);
  119. /* auto-cleanup */
  120. struct __snd_usb_lock {
  121. struct snd_usb_audio *chip;
  122. int err;
  123. };
  124. static inline struct __snd_usb_lock __snd_usb_lock_shutdown(struct snd_usb_audio *chip)
  125. {
  126. struct __snd_usb_lock T = { .chip = chip };
  127. T.err = snd_usb_lock_shutdown(chip);
  128. return T;
  129. }
  130. static inline void __snd_usb_unlock_shutdown(struct __snd_usb_lock *lock)
  131. {
  132. if (!lock->err)
  133. snd_usb_unlock_shutdown(lock->chip);
  134. }
  135. DEFINE_CLASS(snd_usb_lock, struct __snd_usb_lock,
  136. __snd_usb_unlock_shutdown(&(_T)), __snd_usb_lock_shutdown(chip),
  137. struct snd_usb_audio *chip)
  138. extern bool snd_usb_use_vmalloc;
  139. extern bool snd_usb_skip_validation;
  140. /*
  141. * Driver behavior quirk flags, stored in chip->quirk_flags
  142. *
  143. * QUIRK_FLAG_GET_SAMPLE_RATE:
  144. * Skip reading sample rate for devices, as some devices behave inconsistently
  145. * or return error
  146. * QUIRK_FLAG_SHARE_MEDIA_DEVICE:
  147. * Create Media Controller API entries
  148. * QUIRK_FLAG_ALIGN_TRANSFER:
  149. * Allow alignment on audio sub-slot (channel samples) rather than on audio
  150. * slots (audio frames)
  151. * QUIRK_TX_LENGTH:
  152. * Add length specifier to transfers
  153. * QUIRK_FLAG_PLAYBACK_FIRST:
  154. * Start playback stream at first even in implement feedback mode
  155. * QUIRK_FLAG_SKIP_CLOCK_SELECTOR:
  156. * Skip clock selector setup; the device may reset to invalid state
  157. * QUIRK_FLAG_IGNORE_CLOCK_SOURCE:
  158. * Ignore errors from clock source search; i.e. hardcoded clock
  159. * QUIRK_FLAG_ITF_USB_DSD_DAC:
  160. * Indicates the device is for ITF-USB DSD based DACs that need a vendor cmd
  161. * to switch between PCM and native DSD mode
  162. * QUIRK_FLAG_CTL_MSG_DELAY:
  163. * Add a delay of 20ms at each control message handling
  164. * QUIRK_FLAG_CTL_MSG_DELAY_1M:
  165. * Add a delay of 1-2ms at each control message handling
  166. * QUIRK_FLAG_CTL_MSG_DELAY_5M:
  167. * Add a delay of 5-6ms at each control message handling
  168. * QUIRK_FLAG_IFACE_DELAY:
  169. * Add a delay of 50ms at each interface setup
  170. * QUIRK_FLAG_VALIDATE_RATES:
  171. * Perform sample rate validations at probe
  172. * QUIRK_FLAG_DISABLE_AUTOSUSPEND:
  173. * Disable runtime PM autosuspend
  174. * QUIRK_FLAG_IGNORE_CTL_ERROR:
  175. * Ignore errors for mixer access
  176. * QUIRK_FLAG_DSD_RAW:
  177. * Support generic DSD raw U32_BE format
  178. * QUIRK_FLAG_SET_IFACE_FIRST:
  179. * Set up the interface at first like UAC1
  180. * QUIRK_FLAG_GENERIC_IMPLICIT_FB
  181. * Apply the generic implicit feedback sync mode (same as implicit_fb=1 option)
  182. * QUIRK_FLAG_SKIP_IMPLICIT_FB
  183. * Don't apply implicit feedback sync mode
  184. * QUIRK_FLAG_IFACE_SKIP_CLOSE
  185. * Don't closed interface during setting sample rate
  186. * QUIRK_FLAG_FORCE_IFACE_RESET
  187. * Force an interface reset whenever stopping & restarting a stream
  188. * (e.g. after xrun)
  189. * QUIRK_FLAG_FIXED_RATE
  190. * Do not set PCM rate (frequency) when only one rate is available
  191. * for the given endpoint.
  192. * QUIRK_FLAG_MIC_RES_16 and QUIRK_FLAG_MIC_RES_384
  193. * Set the fixed resolution for Mic Capture Volume (mostly for webcams)
  194. * QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE
  195. * Set minimum volume control value as mute for devices where the lowest
  196. * playback value represents muted state instead of minimum audible volume
  197. * QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE
  198. * Similar to QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE, but for capture streams
  199. * QUIRK_FLAG_SKIP_IFACE_SETUP
  200. * Skip the probe-time interface setup (usb_set_interface,
  201. * init_pitch, init_sample_rate); redundant with
  202. * snd_usb_endpoint_prepare() at stream-open time
  203. */
  204. enum {
  205. QUIRK_TYPE_GET_SAMPLE_RATE = 0,
  206. QUIRK_TYPE_SHARE_MEDIA_DEVICE = 1,
  207. QUIRK_TYPE_ALIGN_TRANSFER = 2,
  208. QUIRK_TYPE_TX_LENGTH = 3,
  209. QUIRK_TYPE_PLAYBACK_FIRST = 4,
  210. QUIRK_TYPE_SKIP_CLOCK_SELECTOR = 5,
  211. QUIRK_TYPE_IGNORE_CLOCK_SOURCE = 6,
  212. QUIRK_TYPE_ITF_USB_DSD_DAC = 7,
  213. QUIRK_TYPE_CTL_MSG_DELAY = 8,
  214. QUIRK_TYPE_CTL_MSG_DELAY_1M = 9,
  215. QUIRK_TYPE_CTL_MSG_DELAY_5M = 10,
  216. QUIRK_TYPE_IFACE_DELAY = 11,
  217. QUIRK_TYPE_VALIDATE_RATES = 12,
  218. QUIRK_TYPE_DISABLE_AUTOSUSPEND = 13,
  219. QUIRK_TYPE_IGNORE_CTL_ERROR = 14,
  220. QUIRK_TYPE_DSD_RAW = 15,
  221. QUIRK_TYPE_SET_IFACE_FIRST = 16,
  222. QUIRK_TYPE_GENERIC_IMPLICIT_FB = 17,
  223. QUIRK_TYPE_SKIP_IMPLICIT_FB = 18,
  224. QUIRK_TYPE_IFACE_SKIP_CLOSE = 19,
  225. QUIRK_TYPE_FORCE_IFACE_RESET = 20,
  226. QUIRK_TYPE_FIXED_RATE = 21,
  227. QUIRK_TYPE_MIC_RES_16 = 22,
  228. QUIRK_TYPE_MIC_RES_384 = 23,
  229. QUIRK_TYPE_MIXER_PLAYBACK_MIN_MUTE = 24,
  230. QUIRK_TYPE_MIXER_CAPTURE_MIN_MUTE = 25,
  231. QUIRK_TYPE_SKIP_IFACE_SETUP = 26,
  232. /* Please also edit snd_usb_audio_quirk_flag_names */
  233. };
  234. #define QUIRK_FLAG(x) BIT_U32(QUIRK_TYPE_ ## x)
  235. #define QUIRK_FLAG_GET_SAMPLE_RATE QUIRK_FLAG(GET_SAMPLE_RATE)
  236. #define QUIRK_FLAG_SHARE_MEDIA_DEVICE QUIRK_FLAG(SHARE_MEDIA_DEVICE)
  237. #define QUIRK_FLAG_ALIGN_TRANSFER QUIRK_FLAG(ALIGN_TRANSFER)
  238. #define QUIRK_FLAG_TX_LENGTH QUIRK_FLAG(TX_LENGTH)
  239. #define QUIRK_FLAG_PLAYBACK_FIRST QUIRK_FLAG(PLAYBACK_FIRST)
  240. #define QUIRK_FLAG_SKIP_CLOCK_SELECTOR QUIRK_FLAG(SKIP_CLOCK_SELECTOR)
  241. #define QUIRK_FLAG_IGNORE_CLOCK_SOURCE QUIRK_FLAG(IGNORE_CLOCK_SOURCE)
  242. #define QUIRK_FLAG_ITF_USB_DSD_DAC QUIRK_FLAG(ITF_USB_DSD_DAC)
  243. #define QUIRK_FLAG_CTL_MSG_DELAY QUIRK_FLAG(CTL_MSG_DELAY)
  244. #define QUIRK_FLAG_CTL_MSG_DELAY_1M QUIRK_FLAG(CTL_MSG_DELAY_1M)
  245. #define QUIRK_FLAG_CTL_MSG_DELAY_5M QUIRK_FLAG(CTL_MSG_DELAY_5M)
  246. #define QUIRK_FLAG_IFACE_DELAY QUIRK_FLAG(IFACE_DELAY)
  247. #define QUIRK_FLAG_VALIDATE_RATES QUIRK_FLAG(VALIDATE_RATES)
  248. #define QUIRK_FLAG_DISABLE_AUTOSUSPEND QUIRK_FLAG(DISABLE_AUTOSUSPEND)
  249. #define QUIRK_FLAG_IGNORE_CTL_ERROR QUIRK_FLAG(IGNORE_CTL_ERROR)
  250. #define QUIRK_FLAG_DSD_RAW QUIRK_FLAG(DSD_RAW)
  251. #define QUIRK_FLAG_SET_IFACE_FIRST QUIRK_FLAG(SET_IFACE_FIRST)
  252. #define QUIRK_FLAG_GENERIC_IMPLICIT_FB QUIRK_FLAG(GENERIC_IMPLICIT_FB)
  253. #define QUIRK_FLAG_SKIP_IMPLICIT_FB QUIRK_FLAG(SKIP_IMPLICIT_FB)
  254. #define QUIRK_FLAG_IFACE_SKIP_CLOSE QUIRK_FLAG(IFACE_SKIP_CLOSE)
  255. #define QUIRK_FLAG_FORCE_IFACE_RESET QUIRK_FLAG(FORCE_IFACE_RESET)
  256. #define QUIRK_FLAG_FIXED_RATE QUIRK_FLAG(FIXED_RATE)
  257. #define QUIRK_FLAG_MIC_RES_16 QUIRK_FLAG(MIC_RES_16)
  258. #define QUIRK_FLAG_MIC_RES_384 QUIRK_FLAG(MIC_RES_384)
  259. #define QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE QUIRK_FLAG(MIXER_PLAYBACK_MIN_MUTE)
  260. #define QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE QUIRK_FLAG(MIXER_CAPTURE_MIN_MUTE)
  261. #define QUIRK_FLAG_SKIP_IFACE_SETUP QUIRK_FLAG(SKIP_IFACE_SETUP)
  262. #endif /* __USBAUDIO_H */