Skip to content

Commit 06ffc1e

Browse files
eldadtiwai
authored andcommitted
ALSA: usb-audio: UAC2: do clock validity check earlier
Move the check that parse_audio_format_rates_v2() do after receiving the clock source entity ID directly into the find function and add a validation flag to the function. This patch does not introduce any logic flow change. It is provided to allow introducing automatic clock switching easier later. By moving this uac_clock_source_is_valid callsite, 2 additional callsites can be avoided. Signed-off-by: Eldad Zack <eldad@fogrefinery.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent f6a8bc7 commit 06ffc1e

3 files changed

Lines changed: 21 additions & 18 deletions

File tree

‎sound/usb/clock.c‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
131131
}
132132

133133
static int __uac_clock_find_source(struct snd_usb_audio *chip,
134-
int entity_id, unsigned long *visited)
134+
int entity_id, unsigned long *visited,
135+
bool validate)
135136
{
136137
struct uac_clock_source_descriptor *source;
137138
struct uac_clock_selector_descriptor *selector;
@@ -148,8 +149,15 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
148149

149150
/* first, see if the ID we're looking for is a clock source already */
150151
source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
151-
if (source)
152-
return source->bClockID;
152+
if (source) {
153+
entity_id = source->bClockID;
154+
if (validate && !uac_clock_source_is_valid(chip, entity_id)) {
155+
snd_printk(KERN_ERR "usb-audio:%d: clock source %d is not valid, cannot use\n",
156+
chip->dev->devnum, entity_id);
157+
return -ENXIO;
158+
}
159+
return entity_id;
160+
}
153161

154162
selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
155163
if (selector) {
@@ -164,22 +172,22 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
164172
/* Selector values are one-based */
165173

166174
if (ret > selector->bNrInPins || ret < 1) {
167-
printk(KERN_ERR
175+
snd_printk(KERN_ERR
168176
"%s(): selector reported illegal value, id %d, ret %d\n",
169177
__func__, selector->bClockID, ret);
170178

171179
return -EINVAL;
172180
}
173181

174182
return __uac_clock_find_source(chip, selector->baCSourceID[ret-1],
175-
visited);
183+
visited, validate);
176184
}
177185

178186
/* FIXME: multipliers only act as pass-thru element for now */
179187
multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
180188
if (multiplier)
181189
return __uac_clock_find_source(chip, multiplier->bCSourceID,
182-
visited);
190+
visited, validate);
183191

184192
return -EINVAL;
185193
}
@@ -195,11 +203,12 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
195203
*
196204
* Returns the clock source UnitID (>=0) on success, or an error.
197205
*/
198-
int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id)
206+
int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
207+
bool validate)
199208
{
200209
DECLARE_BITMAP(visited, 256);
201210
memset(visited, 0, sizeof(visited));
202-
return __uac_clock_find_source(chip, entity_id, visited);
211+
return __uac_clock_find_source(chip, entity_id, visited, validate);
203212
}
204213

205214
static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
@@ -275,18 +284,11 @@ static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
275284
struct usb_device *dev = chip->dev;
276285
__le32 data;
277286
int err, cur_rate, prev_rate;
278-
int clock = snd_usb_clock_find_source(chip, fmt->clock);
287+
int clock = snd_usb_clock_find_source(chip, fmt->clock, true);
279288

280289
if (clock < 0)
281290
return clock;
282291

283-
if (!uac_clock_source_is_valid(chip, clock)) {
284-
/* TODO: should we try to find valid clock setups by ourself? */
285-
snd_printk(KERN_ERR "%d:%d:%d: clock source %d is not valid, cannot use\n",
286-
dev->devnum, iface, fmt->altsetting, clock);
287-
return -ENXIO;
288-
}
289-
290292
prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
291293

292294
data = cpu_to_le32(rate);

‎sound/usb/clock.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
55
struct usb_host_interface *alts,
66
struct audioformat *fmt, int rate);
77

8-
int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id);
8+
int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
9+
bool validate);
910

1011
#endif /* __USBAUDIO_CLOCK_H */

‎sound/usb/format.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
280280
struct usb_device *dev = chip->dev;
281281
unsigned char tmp[2], *data;
282282
int nr_triplets, data_size, ret = 0;
283-
int clock = snd_usb_clock_find_source(chip, fp->clock);
283+
int clock = snd_usb_clock_find_source(chip, fp->clock, false);
284284

285285
if (clock < 0) {
286286
snd_printk(KERN_ERR "%s(): unable to find clock source (clock %d)\n",

0 commit comments

Comments
 (0)