• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __USBAUDIO_CARD_H
3 #define __USBAUDIO_CARD_H
4 
5 #include <linux/android_kabi.h>
6 
7 #define MAX_NR_RATES	1024
8 #define MAX_PACKS	6		/* per URB */
9 #define MAX_PACKS_HS	(MAX_PACKS * 8)	/* in high speed mode */
10 #define MAX_URBS	12
11 #define SYNC_URBS	4	/* always four urbs for sync */
12 #define MAX_QUEUE	18	/* try not to exceed this queue length, in ms */
13 
14 struct audioformat {
15 	struct list_head list;
16 	u64 formats;			/* ALSA format bits */
17 	unsigned int channels;		/* # channels */
18 	unsigned int fmt_type;		/* USB audio format type (1-3) */
19 	unsigned int fmt_bits;		/* number of significant bits */
20 	unsigned int frame_size;	/* samples per frame for non-audio */
21 	int iface;			/* interface number */
22 	unsigned char altsetting;	/* corresponding alternate setting */
23 	unsigned char altset_idx;	/* array index of altenate setting */
24 	unsigned char attributes;	/* corresponding attributes of cs endpoint */
25 	unsigned char endpoint;		/* endpoint */
26 	unsigned char ep_attr;		/* endpoint attributes */
27 	unsigned char datainterval;	/* log_2 of data packet interval */
28 	unsigned char protocol;		/* UAC_VERSION_1/2/3 */
29 	unsigned int maxpacksize;	/* max. packet size */
30 	unsigned int rates;		/* rate bitmasks */
31 	unsigned int rate_min, rate_max;	/* min/max rates */
32 	unsigned int nr_rates;		/* number of rate table entries */
33 	unsigned int *rate_table;	/* rate table */
34 	unsigned char clock;		/* associated clock */
35 	struct snd_pcm_chmap_elem *chmap; /* (optional) channel map */
36 	bool dsd_dop;			/* add DOP headers in case of DSD samples */
37 	bool dsd_bitrev;		/* reverse the bits of each DSD sample */
38 	bool dsd_raw;			/* altsetting is raw DSD */
39 };
40 
41 struct snd_usb_substream;
42 struct snd_usb_endpoint;
43 struct snd_usb_power_domain;
44 
45 struct snd_urb_ctx {
46 	struct urb *urb;
47 	unsigned int buffer_size;	/* size of data buffer, if data URB */
48 	struct snd_usb_substream *subs;
49 	struct snd_usb_endpoint *ep;
50 	int index;	/* index for urb array */
51 	int packets;	/* number of packets per urb */
52 	int packet_size[MAX_PACKS_HS]; /* size of packets for next submission */
53 	struct list_head ready_list;
54 };
55 
56 struct snd_usb_endpoint {
57 	struct snd_usb_audio *chip;
58 
59 	int use_count;
60 	int ep_num;		/* the referenced endpoint number */
61 	int type;		/* SND_USB_ENDPOINT_TYPE_* */
62 	unsigned long flags;
63 
64 	void (*prepare_data_urb) (struct snd_usb_substream *subs,
65 				  struct urb *urb);
66 	void (*retire_data_urb) (struct snd_usb_substream *subs,
67 				 struct urb *urb);
68 
69 	struct snd_usb_substream *data_subs;
70 	struct snd_usb_endpoint *sync_master;
71 	struct snd_usb_endpoint *sync_slave;
72 
73 	struct snd_urb_ctx urb[MAX_URBS];
74 
75 	struct snd_usb_packet_info {
76 		uint32_t packet_size[MAX_PACKS_HS];
77 		int packets;
78 	} next_packet[MAX_URBS];
79 	int next_packet_read_pos, next_packet_write_pos;
80 	struct list_head ready_playback_urbs;
81 
82 	unsigned int nurbs;		/* # urbs */
83 	unsigned long active_mask;	/* bitmask of active urbs */
84 	unsigned long unlink_mask;	/* bitmask of unlinked urbs */
85 	char *syncbuf;			/* sync buffer for all sync URBs */
86 	dma_addr_t sync_dma;		/* DMA address of syncbuf */
87 
88 	unsigned int pipe;		/* the data i/o pipe */
89 	unsigned int packsize[2];	/* small/large packet sizes in samples */
90 	unsigned int sample_rem;	/* remainder from division fs/pps */
91 	unsigned int sample_accum;	/* sample accumulator */
92 	unsigned int pps;		/* packets per second */
93 	unsigned int freqn;		/* nominal sampling rate in fs/fps in Q16.16 format */
94 	unsigned int freqm;		/* momentary sampling rate in fs/fps in Q16.16 format */
95 	int	   freqshift;		/* how much to shift the feedback value to get Q16.16 */
96 	unsigned int freqmax;		/* maximum sampling rate, used for buffer management */
97 	unsigned int phase;		/* phase accumulator */
98 	unsigned int maxpacksize;	/* max packet size in bytes */
99 	unsigned int maxframesize;      /* max packet size in frames */
100 	unsigned int max_urb_frames;	/* max URB size in frames */
101 	unsigned int curpacksize;	/* current packet size in bytes (for capture) */
102 	unsigned int curframesize;      /* current packet size in frames (for capture) */
103 	unsigned int syncmaxsize;	/* sync endpoint packet size */
104 	unsigned int fill_max:1;	/* fill max packet size always */
105 	unsigned int tenor_fb_quirk:1;	/* corrupted feedback data */
106 	unsigned int datainterval;      /* log_2 of data packet interval */
107 	unsigned int syncinterval;	/* P for adaptive mode, 0 otherwise */
108 	unsigned char silence_value;
109 	unsigned int stride;
110 	int iface, altsetting;
111 	int skip_packets;		/* quirks for devices to ignore the first n packets
112 					   in a stream */
113 	bool is_implicit_feedback;      /* This endpoint is used as implicit feedback */
114 
115 	spinlock_t lock;
116 	struct list_head list;
117 
118 	ANDROID_KABI_RESERVE(1);
119 	ANDROID_KABI_RESERVE(2);
120 	ANDROID_KABI_RESERVE(3);
121 	ANDROID_KABI_RESERVE(4);
122 };
123 
124 struct media_ctl;
125 
126 struct snd_usb_substream {
127 	struct snd_usb_stream *stream;
128 	struct usb_device *dev;
129 	struct snd_pcm_substream *pcm_substream;
130 	int direction;	/* playback or capture */
131 	int interface;	/* current interface */
132 	int endpoint;	/* assigned endpoint */
133 	struct audioformat *cur_audiofmt;	/* current audioformat pointer (for hw_params callback) */
134 	struct snd_usb_power_domain *str_pd;	/* UAC3 Power Domain for streaming path */
135 	snd_pcm_format_t pcm_format;	/* current audio format (for hw_params callback) */
136 	unsigned int channels;		/* current number of channels (for hw_params callback) */
137 	unsigned int channels_max;	/* max channels in the all audiofmts */
138 	unsigned int cur_rate;		/* current rate (for hw_params callback) */
139 	unsigned int period_bytes;	/* current period bytes (for hw_params callback) */
140 	unsigned int period_frames;	/* current frames per period */
141 	unsigned int buffer_periods;	/* current periods per buffer */
142 	unsigned int altset_idx;     /* USB data format: index of alternate setting */
143 	unsigned int txfr_quirk:1;	/* allow sub-frame alignment */
144 	unsigned int tx_length_quirk:1;	/* add length specifier to transfers */
145 	unsigned int fmt_type;		/* USB audio format type (1-3) */
146 	unsigned int pkt_offset_adj;	/* Bytes to drop from beginning of packets (for non-compliant devices) */
147 	unsigned int stream_offset_adj;	/* Bytes to drop from beginning of stream (for non-compliant devices) */
148 
149 	unsigned int running: 1;	/* running status */
150 
151 	unsigned int hwptr_done;	/* processed byte position in the buffer */
152 	unsigned int transfer_done;		/* processed frames since last period update */
153 	unsigned int frame_limit;	/* limits number of packets in URB */
154 
155 	/* data and sync endpoints for this stream */
156 	unsigned int ep_num;		/* the endpoint number */
157 	struct snd_usb_endpoint *data_endpoint;
158 	struct snd_usb_endpoint *sync_endpoint;
159 	unsigned long flags;
160 	bool need_setup_ep;		/* (re)configure EP at prepare? */
161 	bool need_setup_fmt;		/* (re)configure fmt after resume? */
162 	unsigned int speed;		/* USB_SPEED_XXX */
163 
164 	u64 formats;			/* format bitmasks (all or'ed) */
165 	unsigned int num_formats;		/* number of supported audio formats (list) */
166 	struct list_head fmt_list;	/* format list */
167 	struct snd_pcm_hw_constraint_list rate_list;	/* limited rates */
168 	spinlock_t lock;
169 
170 	int last_frame_number;          /* stored frame number */
171 	int last_delay;                 /* stored delay */
172 
173 	struct {
174 		int marker;
175 		int channel;
176 		int byte_idx;
177 	} dsd_dop;
178 
179 	bool trigger_tstamp_pending_update; /* trigger timestamp being updated from initial estimate */
180 	struct media_ctl *media_ctl;
181 
182 	ANDROID_KABI_RESERVE(1);
183 };
184 
185 struct snd_usb_stream {
186 	struct snd_usb_audio *chip;
187 	struct snd_pcm *pcm;
188 	int pcm_index;
189 	unsigned int fmt_type;		/* USB audio format type (1-3) */
190 	struct snd_usb_substream substream[2];
191 	struct list_head list;
192 };
193 
194 struct snd_usb_substream *find_snd_usb_substream(unsigned int card_num,
195 	unsigned int pcm_idx, unsigned int direction, struct snd_usb_audio
196 	**uchip, void (*disconnect_cb)(struct snd_usb_audio *chip));
197 
198 int snd_vendor_set_ops(struct snd_usb_audio_vendor_ops *vendor_ops);
199 struct snd_usb_audio_vendor_ops *snd_vendor_get_ops(void);
200 int snd_vendor_set_interface(struct usb_device *udev,
201 			     struct usb_host_interface *alts,
202 			     int iface, int alt);
203 int snd_vendor_set_rate(struct usb_interface *intf, int iface, int rate,
204 			int alt);
205 int snd_vendor_set_pcm_buf(struct usb_device *udev, int iface);
206 int snd_vendor_set_pcm_intf(struct usb_interface *intf, int iface, int alt,
207 			    int direction);
208 int snd_vendor_set_pcm_connection(struct usb_device *udev,
209 				  enum snd_vendor_pcm_open_close onoff,
210 				  int direction);
211 int snd_vendor_set_pcm_binterval(struct audioformat *fp,
212 				 struct audioformat *found,
213 				 int *cur_attr, int *attr);
214 
215 #endif /* __USBAUDIO_CARD_H */
216