blob: 87f042d06ce08c47d27c5b07d3351f6183bb2b11 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Daniel Macke5779992010-03-04 19:46:13 +01002#ifndef __USBAUDIO_CARD_H
3#define __USBAUDIO_CARD_H
4
Xi Wang8866f402012-02-14 05:18:48 -05005#define MAX_NR_RATES 1024
Alan Stern976b6c02013-09-24 15:51:58 -04006#define MAX_PACKS 6 /* per URB */
Daniel Macke5779992010-03-04 19:46:13 +01007#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
Alan Stern976b6c02013-09-24 15:51:58 -04008#define MAX_URBS 12
Daniel Macke5779992010-03-04 19:46:13 +01009#define SYNC_URBS 4 /* always four urbs for sync */
Alan Stern976b6c02013-09-24 15:51:58 -040010#define MAX_QUEUE 18 /* try not to exceed this queue length, in ms */
Daniel Macke5779992010-03-04 19:46:13 +010011
12struct audioformat {
13 struct list_head list;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010014 u64 formats; /* ALSA format bits */
Daniel Macke5779992010-03-04 19:46:13 +010015 unsigned int channels; /* # channels */
16 unsigned int fmt_type; /* USB audio format type (1-3) */
Jussi Laakoc7a13262019-02-18 00:17:21 +020017 unsigned int fmt_bits; /* number of significant bits */
Daniel Macke5779992010-03-04 19:46:13 +010018 unsigned int frame_size; /* samples per frame for non-audio */
Takashi Iwai89fa3f62020-11-23 09:53:40 +010019 unsigned char iface; /* interface number */
Daniel Macke5779992010-03-04 19:46:13 +010020 unsigned char altsetting; /* corresponding alternate setting */
Takashi Iwaieae4d052021-01-08 08:52:18 +010021 unsigned char ep_idx; /* endpoint array index */
gushengxianff630b62021-07-05 05:00:52 -070022 unsigned char altset_idx; /* array index of alternate setting */
Daniel Macke5779992010-03-04 19:46:13 +010023 unsigned char attributes; /* corresponding attributes of cs endpoint */
24 unsigned char endpoint; /* endpoint */
25 unsigned char ep_attr; /* endpoint attributes */
Takashi Iwaif6581c02020-11-23 09:53:14 +010026 bool implicit_fb; /* implicit feedback endpoint */
27 unsigned char sync_ep; /* sync endpoint number */
28 unsigned char sync_iface; /* sync EP interface */
29 unsigned char sync_altsetting; /* sync EP alternate setting */
Takashi Iwaibf6313a2020-11-23 09:53:31 +010030 unsigned char sync_ep_idx; /* sync EP array index */
Daniel Macke5779992010-03-04 19:46:13 +010031 unsigned char datainterval; /* log_2 of data packet interval */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020032 unsigned char protocol; /* UAC_VERSION_1/2/3 */
Daniel Macke5779992010-03-04 19:46:13 +010033 unsigned int maxpacksize; /* max. packet size */
34 unsigned int rates; /* rate bitmasks */
35 unsigned int rate_min, rate_max; /* min/max rates */
36 unsigned int nr_rates; /* number of rate table entries */
37 unsigned int *rate_table; /* rate table */
Daniel Mack79f920f2010-05-31 14:51:31 +020038 unsigned char clock; /* associated clock */
Takashi Iwai04324cc2012-11-26 16:24:02 +010039 struct snd_pcm_chmap_elem *chmap; /* (optional) channel map */
Daniel Mackd24f5062013-04-17 00:01:38 +080040 bool dsd_dop; /* add DOP headers in case of DSD samples */
Daniel Mack44dcbbb2013-04-17 00:01:39 +080041 bool dsd_bitrev; /* reverse the bits of each DSD sample */
Jussi Laako3a572d92018-06-13 01:43:01 +030042 bool dsd_raw; /* altsetting is raw DSD */
Daniel Macke5779992010-03-04 19:46:13 +010043};
44
45struct snd_usb_substream;
Takashi Iwai00272c62021-01-08 08:52:17 +010046struct snd_usb_iface_ref;
Daniel Mack8fdff6a2012-04-12 13:51:11 +020047struct snd_usb_endpoint;
Jorge Sanjuan7edf3b52018-07-31 13:28:43 +010048struct snd_usb_power_domain;
Daniel Macke5779992010-03-04 19:46:13 +010049
50struct snd_urb_ctx {
51 struct urb *urb;
52 unsigned int buffer_size; /* size of data buffer, if data URB */
53 struct snd_usb_substream *subs;
Daniel Mack8fdff6a2012-04-12 13:51:11 +020054 struct snd_usb_endpoint *ep;
Daniel Macke5779992010-03-04 19:46:13 +010055 int index; /* index for urb array */
56 int packets; /* number of packets per urb */
Takashi Iwaie8a8f092021-06-01 18:24:55 +020057 int queued; /* queued data bytes by this urb */
Daniel Mack8fdff6a2012-04-12 13:51:11 +020058 int packet_size[MAX_PACKS_HS]; /* size of packets for next submission */
59 struct list_head ready_list;
Daniel Macke5779992010-03-04 19:46:13 +010060};
61
Daniel Mack8fdff6a2012-04-12 13:51:11 +020062struct snd_usb_endpoint {
63 struct snd_usb_audio *chip;
Takashi Iwai00272c62021-01-08 08:52:17 +010064 struct snd_usb_iface_ref *iface_ref;
Daniel Mack8fdff6a2012-04-12 13:51:11 +020065
Takashi Iwaibf6313a2020-11-23 09:53:31 +010066 int opened; /* open refcount; protect with chip->mutex */
Takashi Iwai43b81e82020-11-23 09:53:34 +010067 atomic_t running; /* running status */
Daniel Mack8fdff6a2012-04-12 13:51:11 +020068 int ep_num; /* the referenced endpoint number */
69 int type; /* SND_USB_ENDPOINT_TYPE_* */
Takashi Iwai89fa3f62020-11-23 09:53:40 +010070
71 unsigned char iface; /* interface number */
72 unsigned char altsetting; /* corresponding alternate setting */
73 unsigned char ep_idx; /* endpoint array index */
74
Takashi Iwai5c2b3012021-02-06 21:30:51 +010075 atomic_t state; /* running state */
Daniel Mack8fdff6a2012-04-12 13:51:11 +020076
Takashi Iwaid5f871f2021-09-29 10:08:43 +020077 int (*prepare_data_urb) (struct snd_usb_substream *subs,
78 struct urb *urb,
79 bool in_stream_lock);
Daniel Mack8fdff6a2012-04-12 13:51:11 +020080 void (*retire_data_urb) (struct snd_usb_substream *subs,
81 struct urb *urb);
82
83 struct snd_usb_substream *data_subs;
Takashi Iwai53837b42020-11-23 09:53:39 +010084 struct snd_usb_endpoint *sync_source;
85 struct snd_usb_endpoint *sync_sink;
Daniel Mack8fdff6a2012-04-12 13:51:11 +020086
87 struct snd_urb_ctx urb[MAX_URBS];
88
89 struct snd_usb_packet_info {
90 uint32_t packet_size[MAX_PACKS_HS];
91 int packets;
92 } next_packet[MAX_URBS];
Takashi Iwaic15871e2020-11-23 09:53:32 +010093 unsigned int next_packet_head; /* ring buffer offset to read */
94 unsigned int next_packet_queued; /* queued items in the ring buffer */
95 struct list_head ready_playback_urbs; /* playback URB FIFO for implicit fb */
Daniel Mack8fdff6a2012-04-12 13:51:11 +020096
97 unsigned int nurbs; /* # urbs */
98 unsigned long active_mask; /* bitmask of active urbs */
99 unsigned long unlink_mask; /* bitmask of unlinked urbs */
Takashi Iwai86a42ad2021-09-29 10:08:37 +0200100 atomic_t submitted_urbs; /* currently submitted urbs */
Daniel Mack8fdff6a2012-04-12 13:51:11 +0200101 char *syncbuf; /* sync buffer for all sync URBs */
102 dma_addr_t sync_dma; /* DMA address of syncbuf */
103
104 unsigned int pipe; /* the data i/o pipe */
Alexander Tsoyb9fd2002020-06-29 05:59:34 +0300105 unsigned int packsize[2]; /* small/large packet sizes in samples */
106 unsigned int sample_rem; /* remainder from division fs/pps */
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +0300107 unsigned int sample_accum; /* sample accumulator */
Alexander Tsoyb9fd2002020-06-29 05:59:34 +0300108 unsigned int pps; /* packets per second */
Daniel Mack8fdff6a2012-04-12 13:51:11 +0200109 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
110 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
111 int freqshift; /* how much to shift the feedback value to get Q16.16 */
112 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
113 unsigned int phase; /* phase accumulator */
114 unsigned int maxpacksize; /* max packet size in bytes */
115 unsigned int maxframesize; /* max packet size in frames */
Alan Stern976b6c02013-09-24 15:51:58 -0400116 unsigned int max_urb_frames; /* max URB size in frames */
Daniel Mack8fdff6a2012-04-12 13:51:11 +0200117 unsigned int curpacksize; /* current packet size in bytes (for capture) */
118 unsigned int curframesize; /* current packet size in frames (for capture) */
119 unsigned int syncmaxsize; /* sync endpoint packet size */
120 unsigned int fill_max:1; /* fill max packet size always */
Daniel Mackca0dd272016-08-22 08:53:37 +0200121 unsigned int tenor_fb_quirk:1; /* corrupted feedback data */
Daniel Mack8fdff6a2012-04-12 13:51:11 +0200122 unsigned int datainterval; /* log_2 of data packet interval */
123 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
124 unsigned char silence_value;
125 unsigned int stride;
Daniel Mack2b58fd52012-09-04 10:23:07 +0200126 int skip_packets; /* quirks for devices to ignore the first n packets
127 in a stream */
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100128 bool implicit_fb_sync; /* syncs with implicit feedback */
Takashi Iwai9c9a3b92021-09-29 10:08:38 +0200129 bool lowlatency_playback; /* low-latency playback mode */
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100130 bool need_setup; /* (re-)need for configure? */
Daniel Mack8fdff6a2012-04-12 13:51:11 +0200131
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100132 /* for hw constraints */
Takashi Iwaicab941b2020-11-23 09:53:33 +0100133 const struct audioformat *cur_audiofmt;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100134 unsigned int cur_rate;
135 snd_pcm_format_t cur_format;
136 unsigned int cur_channels;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100137 unsigned int cur_frame_bytes;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100138 unsigned int cur_period_frames;
139 unsigned int cur_period_bytes;
140 unsigned int cur_buffer_periods;
Takashi Iwai4e7cf1f2021-09-29 10:08:36 +0200141 unsigned char cur_clock;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100142
Daniel Mack8fdff6a2012-04-12 13:51:11 +0200143 spinlock_t lock;
144 struct list_head list;
145};
146
Shuah Khan66354f12019-04-01 20:40:22 -0400147struct media_ctl;
148
Daniel Macke5779992010-03-04 19:46:13 +0100149struct snd_usb_substream {
150 struct snd_usb_stream *stream;
151 struct usb_device *dev;
152 struct snd_pcm_substream *pcm_substream;
153 int direction; /* playback or capture */
Daniel Macke5779992010-03-04 19:46:13 +0100154 int endpoint; /* assigned endpoint */
Takashi Iwaicab941b2020-11-23 09:53:33 +0100155 const struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
Jorge Sanjuan7edf3b52018-07-31 13:28:43 +0100156 struct snd_usb_power_domain *str_pd; /* UAC3 Power Domain for streaming path */
Takashi Iwai04324cc2012-11-26 16:24:02 +0100157 unsigned int channels_max; /* max channels in the all audiofmts */
Daniel Macke5779992010-03-04 19:46:13 +0100158 unsigned int txfr_quirk:1; /* allow sub-frame alignment */
Ricard Wanderlofe0570442015-10-19 08:52:53 +0200159 unsigned int tx_length_quirk:1; /* add length specifier to transfers */
Daniel Macke5779992010-03-04 19:46:13 +0100160 unsigned int fmt_type; /* USB audio format type (1-3) */
Calvin Owens1539d4f2013-04-12 22:33:59 -0500161 unsigned int pkt_offset_adj; /* Bytes to drop from beginning of packets (for non-compliant devices) */
Hector Martin1b7ecc22020-08-10 17:24:00 +0900162 unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream (for non-compliant devices) */
Daniel Macke5779992010-03-04 19:46:13 +0100163
164 unsigned int running: 1; /* running status */
Takashi Iwai307cc9b2021-07-07 13:24:47 +0200165 unsigned int period_elapsed_pending; /* delay period handling */
Daniel Macke5779992010-03-04 19:46:13 +0100166
Takashi Iwaid303c5d2021-06-01 18:24:54 +0200167 unsigned int buffer_bytes; /* buffer size in bytes */
Takashi Iwaie8a8f092021-06-01 18:24:55 +0200168 unsigned int inflight_bytes; /* in-flight data bytes on buffer (for playback) */
Daniel Macke5779992010-03-04 19:46:13 +0100169 unsigned int hwptr_done; /* processed byte position in the buffer */
Takashi Iwaie8a8f092021-06-01 18:24:55 +0200170 unsigned int transfer_done; /* processed frames since last period update */
Alan Stern976b6c02013-09-24 15:51:58 -0400171 unsigned int frame_limit; /* limits number of packets in URB */
Daniel Macke5779992010-03-04 19:46:13 +0100172
Daniel Mackedcd3632012-04-12 13:51:12 +0200173 /* data and sync endpoints for this stream */
Takashi Iwai8260ef02012-06-08 09:01:37 +0200174 unsigned int ep_num; /* the endpoint number */
Daniel Mackedcd3632012-04-12 13:51:12 +0200175 struct snd_usb_endpoint *data_endpoint;
176 struct snd_usb_endpoint *sync_endpoint;
177 unsigned long flags;
Takashi Iwai978520b2012-10-12 15:12:55 +0200178 unsigned int speed; /* USB_SPEED_XXX */
Daniel Macke5779992010-03-04 19:46:13 +0100179
180 u64 formats; /* format bitmasks (all or'ed) */
181 unsigned int num_formats; /* number of supported audio formats (list) */
182 struct list_head fmt_list; /* format list */
Daniel Macke5779992010-03-04 19:46:13 +0100183 spinlock_t lock;
184
Takashi Iwaie8a8f092021-06-01 18:24:55 +0200185 unsigned int last_frame_number; /* stored frame number */
Daniel Mackd24f5062013-04-17 00:01:38 +0800186
187 struct {
188 int marker;
189 int channel;
190 int byte_idx;
191 } dsd_dop;
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -0600192
193 bool trigger_tstamp_pending_update; /* trigger timestamp being updated from initial estimate */
Takashi Iwai9c9a3b92021-09-29 10:08:38 +0200194 bool lowlatency_playback; /* low-latency playback mode */
Shuah Khan66354f12019-04-01 20:40:22 -0400195 struct media_ctl *media_ctl;
Daniel Macke5779992010-03-04 19:46:13 +0100196};
197
198struct snd_usb_stream {
199 struct snd_usb_audio *chip;
200 struct snd_pcm *pcm;
201 int pcm_index;
202 unsigned int fmt_type; /* USB audio format type (1-3) */
203 struct snd_usb_substream substream[2];
204 struct list_head list;
205};
206
207#endif /* __USBAUDIO_CARD_H */