Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Clock domain and sample rate management functions |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/init.h> |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 22 | #include <linux/string.h> |
| 23 | #include <linux/usb.h> |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 24 | #include <linux/usb/audio.h> |
| 25 | #include <linux/usb/audio-v2.h> |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 26 | #include <linux/usb/audio-v3.h> |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 27 | |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/info.h> |
| 30 | #include <sound/pcm.h> |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 31 | |
| 32 | #include "usbaudio.h" |
| 33 | #include "card.h" |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 34 | #include "helper.h" |
Daniel Mack | f22aa94 | 2010-06-16 17:57:27 +0200 | [diff] [blame] | 35 | #include "clock.h" |
Daniel Mack | 21bb5aa | 2013-04-10 00:56:03 +0800 | [diff] [blame] | 36 | #include "quirks.h" |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 37 | |
| 38 | static struct uac_clock_source_descriptor * |
| 39 | snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface, |
| 40 | int clock_id) |
| 41 | { |
| 42 | struct uac_clock_source_descriptor *cs = NULL; |
| 43 | |
| 44 | while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra, |
| 45 | ctrl_iface->extralen, |
| 46 | cs, UAC2_CLOCK_SOURCE))) { |
Takashi Iwai | 0a62d6c | 2017-11-21 17:28:06 +0100 | [diff] [blame] | 47 | if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 48 | return cs; |
| 49 | } |
| 50 | |
| 51 | return NULL; |
| 52 | } |
| 53 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 54 | static struct uac3_clock_source_descriptor * |
| 55 | snd_usb_find_clock_source_v3(struct usb_host_interface *ctrl_iface, |
| 56 | int clock_id) |
| 57 | { |
| 58 | struct uac3_clock_source_descriptor *cs = NULL; |
| 59 | |
| 60 | while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra, |
| 61 | ctrl_iface->extralen, |
| 62 | cs, UAC3_CLOCK_SOURCE))) { |
| 63 | if (cs->bClockID == clock_id) |
| 64 | return cs; |
| 65 | } |
| 66 | |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 70 | static struct uac_clock_selector_descriptor * |
| 71 | snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface, |
| 72 | int clock_id) |
| 73 | { |
| 74 | struct uac_clock_selector_descriptor *cs = NULL; |
| 75 | |
| 76 | while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra, |
| 77 | ctrl_iface->extralen, |
| 78 | cs, UAC2_CLOCK_SELECTOR))) { |
Takashi Iwai | 0a62d6c | 2017-11-21 17:28:06 +0100 | [diff] [blame] | 79 | if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) { |
| 80 | if (cs->bLength < 5 + cs->bNrInPins) |
| 81 | return NULL; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 82 | return cs; |
Takashi Iwai | 0a62d6c | 2017-11-21 17:28:06 +0100 | [diff] [blame] | 83 | } |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
| 88 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 89 | static struct uac3_clock_selector_descriptor * |
| 90 | snd_usb_find_clock_selector_v3(struct usb_host_interface *ctrl_iface, |
| 91 | int clock_id) |
| 92 | { |
| 93 | struct uac3_clock_selector_descriptor *cs = NULL; |
| 94 | |
| 95 | while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra, |
| 96 | ctrl_iface->extralen, |
| 97 | cs, UAC3_CLOCK_SELECTOR))) { |
| 98 | if (cs->bClockID == clock_id) |
| 99 | return cs; |
| 100 | } |
| 101 | |
| 102 | return NULL; |
| 103 | } |
| 104 | |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 105 | static struct uac_clock_multiplier_descriptor * |
| 106 | snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface, |
| 107 | int clock_id) |
| 108 | { |
| 109 | struct uac_clock_multiplier_descriptor *cs = NULL; |
| 110 | |
| 111 | while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra, |
| 112 | ctrl_iface->extralen, |
| 113 | cs, UAC2_CLOCK_MULTIPLIER))) { |
Takashi Iwai | 0a62d6c | 2017-11-21 17:28:06 +0100 | [diff] [blame] | 114 | if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 115 | return cs; |
| 116 | } |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 121 | static struct uac3_clock_multiplier_descriptor * |
| 122 | snd_usb_find_clock_multiplier_v3(struct usb_host_interface *ctrl_iface, |
| 123 | int clock_id) |
| 124 | { |
| 125 | struct uac3_clock_multiplier_descriptor *cs = NULL; |
| 126 | |
| 127 | while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra, |
| 128 | ctrl_iface->extralen, |
| 129 | cs, UAC3_CLOCK_MULTIPLIER))) { |
| 130 | if (cs->bClockID == clock_id) |
| 131 | return cs; |
| 132 | } |
| 133 | |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 137 | static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id) |
| 138 | { |
| 139 | unsigned char buf; |
| 140 | int ret; |
| 141 | |
| 142 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), |
| 143 | UAC2_CS_CUR, |
| 144 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
Daniel Mack | 11bcbc4 | 2010-06-11 17:34:20 +0200 | [diff] [blame] | 145 | UAC2_CX_CLOCK_SELECTOR << 8, |
| 146 | snd_usb_ctrl_intf(chip) | (selector_id << 8), |
Clemens Ladisch | 17d900c | 2011-09-26 21:15:27 +0200 | [diff] [blame] | 147 | &buf, sizeof(buf)); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 148 | |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | |
| 152 | return buf; |
| 153 | } |
| 154 | |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 155 | static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id, |
| 156 | unsigned char pin) |
| 157 | { |
| 158 | int ret; |
| 159 | |
| 160 | ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), |
| 161 | UAC2_CS_CUR, |
| 162 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, |
| 163 | UAC2_CX_CLOCK_SELECTOR << 8, |
| 164 | snd_usb_ctrl_intf(chip) | (selector_id << 8), |
| 165 | &pin, sizeof(pin)); |
| 166 | if (ret < 0) |
| 167 | return ret; |
| 168 | |
| 169 | if (ret != sizeof(pin)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 170 | usb_audio_err(chip, |
| 171 | "setting selector (id %d) unexpected length %d\n", |
| 172 | selector_id, ret); |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | ret = uac_clock_selector_get_val(chip, selector_id); |
| 177 | if (ret < 0) |
| 178 | return ret; |
| 179 | |
| 180 | if (ret != pin) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 181 | usb_audio_err(chip, |
| 182 | "setting selector (id %d) to %x failed (current: %d)\n", |
| 183 | selector_id, pin, ret); |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | } |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 190 | static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, |
| 191 | int protocol, |
| 192 | int source_id) |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 193 | { |
| 194 | int err; |
| 195 | unsigned char data; |
| 196 | struct usb_device *dev = chip->dev; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 197 | u32 bmControls; |
Daniel Mack | 3bc6fbc7 | 2011-05-18 11:28:44 +0200 | [diff] [blame] | 198 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 199 | if (protocol == UAC_VERSION_3) { |
| 200 | struct uac3_clock_source_descriptor *cs_desc = |
| 201 | snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id); |
| 202 | |
| 203 | if (!cs_desc) |
| 204 | return 0; |
| 205 | bmControls = le32_to_cpu(cs_desc->bmControls); |
| 206 | } else { /* UAC_VERSION_1/2 */ |
| 207 | struct uac_clock_source_descriptor *cs_desc = |
| 208 | snd_usb_find_clock_source(chip->ctrl_intf, source_id); |
| 209 | |
| 210 | if (!cs_desc) |
| 211 | return 0; |
| 212 | bmControls = cs_desc->bmControls; |
| 213 | } |
Daniel Mack | 3bc6fbc7 | 2011-05-18 11:28:44 +0200 | [diff] [blame] | 214 | |
| 215 | /* If a clock source can't tell us whether it's valid, we assume it is */ |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 216 | if (!uac_v2v3_control_is_readable(bmControls, |
Daniel Mack | aff252a | 2012-08-01 10:16:53 +0200 | [diff] [blame] | 217 | UAC2_CS_CONTROL_CLOCK_VALID - 1)) |
Daniel Mack | 3bc6fbc7 | 2011-05-18 11:28:44 +0200 | [diff] [blame] | 218 | return 1; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 219 | |
| 220 | err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, |
| 221 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
Daniel Mack | 11bcbc4 | 2010-06-11 17:34:20 +0200 | [diff] [blame] | 222 | UAC2_CS_CONTROL_CLOCK_VALID << 8, |
| 223 | snd_usb_ctrl_intf(chip) | (source_id << 8), |
Clemens Ladisch | 17d900c | 2011-09-26 21:15:27 +0200 | [diff] [blame] | 224 | &data, sizeof(data)); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 225 | |
| 226 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 227 | dev_warn(&dev->dev, |
| 228 | "%s(): cannot get clock validity for id %d\n", |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 229 | __func__, source_id); |
Daniel Mack | 3bc6fbc7 | 2011-05-18 11:28:44 +0200 | [diff] [blame] | 230 | return 0; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return !!data; |
| 234 | } |
| 235 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 236 | static int __uac_clock_find_source(struct snd_usb_audio *chip, int entity_id, |
| 237 | unsigned long *visited, bool validate) |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 238 | { |
| 239 | struct uac_clock_source_descriptor *source; |
| 240 | struct uac_clock_selector_descriptor *selector; |
| 241 | struct uac_clock_multiplier_descriptor *multiplier; |
| 242 | |
| 243 | entity_id &= 0xff; |
| 244 | |
| 245 | if (test_and_set_bit(entity_id, visited)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 246 | usb_audio_warn(chip, |
| 247 | "%s(): recursive clock topology detected, id %d.\n", |
| 248 | __func__, entity_id); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | /* first, see if the ID we're looking for is a clock source already */ |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 253 | source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id); |
Eldad Zack | 06ffc1e | 2013-04-03 23:18:54 +0200 | [diff] [blame] | 254 | if (source) { |
| 255 | entity_id = source->bClockID; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 256 | if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_2, |
| 257 | entity_id)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 258 | usb_audio_err(chip, |
| 259 | "clock source %d is not valid, cannot use\n", |
| 260 | entity_id); |
Eldad Zack | 06ffc1e | 2013-04-03 23:18:54 +0200 | [diff] [blame] | 261 | return -ENXIO; |
| 262 | } |
| 263 | return entity_id; |
| 264 | } |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 265 | |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 266 | selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 267 | if (selector) { |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 268 | int ret, i, cur; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 269 | |
| 270 | /* the entity ID we are looking for is a selector. |
| 271 | * find out what it currently selects */ |
| 272 | ret = uac_clock_selector_get_val(chip, selector->bClockID); |
| 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | |
Daniel Mack | 157a57b | 2010-06-16 17:57:30 +0200 | [diff] [blame] | 276 | /* Selector values are one-based */ |
| 277 | |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 278 | if (ret > selector->bNrInPins || ret < 1) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 279 | usb_audio_err(chip, |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 280 | "%s(): selector reported illegal value, id %d, ret %d\n", |
| 281 | __func__, selector->bClockID, ret); |
| 282 | |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 286 | cur = ret; |
| 287 | ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1], |
Eldad Zack | 06ffc1e | 2013-04-03 23:18:54 +0200 | [diff] [blame] | 288 | visited, validate); |
Eldad Zack | ef02e29 | 2013-04-03 23:18:56 +0200 | [diff] [blame] | 289 | if (!validate || ret > 0 || !chip->autoclock) |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 290 | return ret; |
| 291 | |
| 292 | /* The current clock source is invalid, try others. */ |
| 293 | for (i = 1; i <= selector->bNrInPins; i++) { |
| 294 | int err; |
| 295 | |
| 296 | if (i == cur) |
| 297 | continue; |
| 298 | |
| 299 | ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1], |
| 300 | visited, true); |
| 301 | if (ret < 0) |
| 302 | continue; |
| 303 | |
| 304 | err = uac_clock_selector_set_val(chip, entity_id, i); |
| 305 | if (err < 0) |
| 306 | continue; |
| 307 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 308 | usb_audio_info(chip, |
| 309 | "found and selected valid clock source %d\n", |
| 310 | ret); |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | return -ENXIO; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* FIXME: multipliers only act as pass-thru element for now */ |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 318 | multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 319 | if (multiplier) |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 320 | return __uac_clock_find_source(chip, multiplier->bCSourceID, |
Eldad Zack | 06ffc1e | 2013-04-03 23:18:54 +0200 | [diff] [blame] | 321 | visited, validate); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 322 | |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 326 | static int __uac3_clock_find_source(struct snd_usb_audio *chip, int entity_id, |
| 327 | unsigned long *visited, bool validate) |
| 328 | { |
| 329 | struct uac3_clock_source_descriptor *source; |
| 330 | struct uac3_clock_selector_descriptor *selector; |
| 331 | struct uac3_clock_multiplier_descriptor *multiplier; |
| 332 | |
| 333 | entity_id &= 0xff; |
| 334 | |
| 335 | if (test_and_set_bit(entity_id, visited)) { |
| 336 | usb_audio_warn(chip, |
| 337 | "%s(): recursive clock topology detected, id %d.\n", |
| 338 | __func__, entity_id); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | /* first, see if the ID we're looking for is a clock source already */ |
| 343 | source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id); |
| 344 | if (source) { |
| 345 | entity_id = source->bClockID; |
| 346 | if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_3, |
| 347 | entity_id)) { |
| 348 | usb_audio_err(chip, |
| 349 | "clock source %d is not valid, cannot use\n", |
| 350 | entity_id); |
| 351 | return -ENXIO; |
| 352 | } |
| 353 | return entity_id; |
| 354 | } |
| 355 | |
| 356 | selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id); |
| 357 | if (selector) { |
| 358 | int ret, i, cur; |
| 359 | |
| 360 | /* the entity ID we are looking for is a selector. |
| 361 | * find out what it currently selects */ |
| 362 | ret = uac_clock_selector_get_val(chip, selector->bClockID); |
| 363 | if (ret < 0) |
| 364 | return ret; |
| 365 | |
| 366 | /* Selector values are one-based */ |
| 367 | |
| 368 | if (ret > selector->bNrInPins || ret < 1) { |
| 369 | usb_audio_err(chip, |
| 370 | "%s(): selector reported illegal value, id %d, ret %d\n", |
| 371 | __func__, selector->bClockID, ret); |
| 372 | |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | |
| 376 | cur = ret; |
| 377 | ret = __uac3_clock_find_source(chip, selector->baCSourceID[ret - 1], |
| 378 | visited, validate); |
| 379 | if (!validate || ret > 0 || !chip->autoclock) |
| 380 | return ret; |
| 381 | |
| 382 | /* The current clock source is invalid, try others. */ |
| 383 | for (i = 1; i <= selector->bNrInPins; i++) { |
| 384 | int err; |
| 385 | |
| 386 | if (i == cur) |
| 387 | continue; |
| 388 | |
| 389 | ret = __uac3_clock_find_source(chip, selector->baCSourceID[i - 1], |
| 390 | visited, true); |
| 391 | if (ret < 0) |
| 392 | continue; |
| 393 | |
| 394 | err = uac_clock_selector_set_val(chip, entity_id, i); |
| 395 | if (err < 0) |
| 396 | continue; |
| 397 | |
| 398 | usb_audio_info(chip, |
| 399 | "found and selected valid clock source %d\n", |
| 400 | ret); |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | return -ENXIO; |
| 405 | } |
| 406 | |
| 407 | /* FIXME: multipliers only act as pass-thru element for now */ |
| 408 | multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf, |
| 409 | entity_id); |
| 410 | if (multiplier) |
| 411 | return __uac3_clock_find_source(chip, multiplier->bCSourceID, |
| 412 | visited, validate); |
| 413 | |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
Daniel Mack | 157a57b | 2010-06-16 17:57:30 +0200 | [diff] [blame] | 417 | /* |
| 418 | * For all kinds of sample rate settings and other device queries, |
| 419 | * the clock source (end-leaf) must be used. However, clock selectors, |
| 420 | * clock multipliers and sample rate converters may be specified as |
| 421 | * clock source input to terminal. This functions walks the clock path |
| 422 | * to its end and tries to find the source. |
| 423 | * |
| 424 | * The 'visited' bitfield is used internally to detect recursive loops. |
| 425 | * |
| 426 | * Returns the clock source UnitID (>=0) on success, or an error. |
| 427 | */ |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 428 | int snd_usb_clock_find_source(struct snd_usb_audio *chip, int protocol, |
| 429 | int entity_id, bool validate) |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 430 | { |
| 431 | DECLARE_BITMAP(visited, 256); |
| 432 | memset(visited, 0, sizeof(visited)); |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 433 | |
| 434 | switch (protocol) { |
| 435 | case UAC_VERSION_2: |
| 436 | return __uac_clock_find_source(chip, entity_id, visited, |
| 437 | validate); |
| 438 | case UAC_VERSION_3: |
| 439 | return __uac3_clock_find_source(chip, entity_id, visited, |
| 440 | validate); |
| 441 | default: |
| 442 | return -EINVAL; |
| 443 | } |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface, |
| 447 | struct usb_host_interface *alts, |
| 448 | struct audioformat *fmt, int rate) |
| 449 | { |
| 450 | struct usb_device *dev = chip->dev; |
| 451 | unsigned int ep; |
| 452 | unsigned char data[3]; |
| 453 | int err, crate; |
| 454 | |
Takashi Iwai | 447d627 | 2016-03-15 15:20:58 +0100 | [diff] [blame] | 455 | if (get_iface_desc(alts)->bNumEndpoints < 1) |
| 456 | return -EINVAL; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 457 | ep = get_endpoint(alts, 0)->bEndpointAddress; |
| 458 | |
| 459 | /* if endpoint doesn't have sampling rate control, bail out */ |
Clemens Ladisch | d32d552 | 2010-07-08 16:38:01 +0200 | [diff] [blame] | 460 | if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 461 | return 0; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 462 | |
| 463 | data[0] = rate; |
| 464 | data[1] = rate >> 8; |
| 465 | data[2] = rate >> 16; |
| 466 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, |
| 467 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, |
| 468 | UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, |
Clemens Ladisch | 17d900c | 2011-09-26 21:15:27 +0200 | [diff] [blame] | 469 | data, sizeof(data))) < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 470 | dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n", |
| 471 | iface, fmt->altsetting, rate, ep); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 472 | return err; |
| 473 | } |
| 474 | |
Joe Turner | b62b998 | 2015-02-16 20:44:33 +0000 | [diff] [blame] | 475 | /* Don't check the sample rate for devices which we know don't |
| 476 | * support reading */ |
| 477 | if (snd_usb_get_sample_rate_quirk(chip)) |
| 478 | return 0; |
Takashi Iwai | 57dd541 | 2016-04-29 11:49:04 +0200 | [diff] [blame] | 479 | /* the firmware is likely buggy, don't repeat to fail too many times */ |
| 480 | if (chip->sample_rate_read_error > 2) |
| 481 | return 0; |
Joe Turner | b62b998 | 2015-02-16 20:44:33 +0000 | [diff] [blame] | 482 | |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 483 | if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR, |
| 484 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN, |
| 485 | UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, |
Clemens Ladisch | 17d900c | 2011-09-26 21:15:27 +0200 | [diff] [blame] | 486 | data, sizeof(data))) < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 487 | dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n", |
| 488 | iface, fmt->altsetting, ep); |
Takashi Iwai | 57dd541 | 2016-04-29 11:49:04 +0200 | [diff] [blame] | 489 | chip->sample_rate_read_error++; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 490 | return 0; /* some devices don't support reading */ |
| 491 | } |
| 492 | |
| 493 | crate = data[0] | (data[1] << 8) | (data[2] << 16); |
| 494 | if (crate != rate) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 495 | dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 496 | // runtime->rate = crate; |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 502 | static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface, |
Takashi Iwai | 7c517465 | 2013-04-03 19:08:29 +0200 | [diff] [blame] | 503 | int altsetting, int clock) |
| 504 | { |
| 505 | struct usb_device *dev = chip->dev; |
Eldad Zack | f6a8bc7 | 2013-04-03 23:18:53 +0200 | [diff] [blame] | 506 | __le32 data; |
Takashi Iwai | 7c517465 | 2013-04-03 19:08:29 +0200 | [diff] [blame] | 507 | int err; |
| 508 | |
| 509 | err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, |
| 510 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
| 511 | UAC2_CS_CONTROL_SAM_FREQ << 8, |
| 512 | snd_usb_ctrl_intf(chip) | (clock << 8), |
Eldad Zack | f6a8bc7 | 2013-04-03 23:18:53 +0200 | [diff] [blame] | 513 | &data, sizeof(data)); |
Takashi Iwai | 7c517465 | 2013-04-03 19:08:29 +0200 | [diff] [blame] | 514 | if (err < 0) { |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 515 | dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n", |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 516 | iface, altsetting, err); |
Takashi Iwai | 7c517465 | 2013-04-03 19:08:29 +0200 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Eldad Zack | f6a8bc7 | 2013-04-03 23:18:53 +0200 | [diff] [blame] | 520 | return le32_to_cpu(data); |
Takashi Iwai | 7c517465 | 2013-04-03 19:08:29 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 523 | static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface, |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 524 | struct usb_host_interface *alts, |
| 525 | struct audioformat *fmt, int rate) |
| 526 | { |
| 527 | struct usb_device *dev = chip->dev; |
Eldad Zack | f6a8bc7 | 2013-04-03 23:18:53 +0200 | [diff] [blame] | 528 | __le32 data; |
Torstein Hegge | 690a863 | 2013-03-26 22:10:05 +0100 | [diff] [blame] | 529 | int err, cur_rate, prev_rate; |
Eldad Zack | 8c55af3 | 2013-04-03 23:18:55 +0200 | [diff] [blame] | 530 | int clock; |
Eldad Zack | 1dc669f | 2013-04-03 23:18:58 +0200 | [diff] [blame] | 531 | bool writeable; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 532 | u32 bmControls; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 533 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 534 | clock = snd_usb_clock_find_source(chip, fmt->protocol, |
| 535 | fmt->clock, true); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 536 | if (clock < 0) |
| 537 | return clock; |
| 538 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 539 | prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock); |
David Henningsson | fa92dd7 | 2013-04-25 14:32:24 +0200 | [diff] [blame] | 540 | if (prev_rate == rate) |
| 541 | return 0; |
Torstein Hegge | 690a863 | 2013-03-26 22:10:05 +0100 | [diff] [blame] | 542 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 543 | if (fmt->protocol == UAC_VERSION_3) { |
| 544 | struct uac3_clock_source_descriptor *cs_desc; |
| 545 | |
| 546 | cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock); |
| 547 | bmControls = le32_to_cpu(cs_desc->bmControls); |
| 548 | } else { |
| 549 | struct uac_clock_source_descriptor *cs_desc; |
| 550 | |
| 551 | cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock); |
| 552 | bmControls = cs_desc->bmControls; |
| 553 | } |
| 554 | |
| 555 | writeable = uac_v2v3_control_is_writeable(bmControls, UAC2_CS_CONTROL_SAM_FREQ - 1); |
Eldad Zack | 1dc669f | 2013-04-03 23:18:58 +0200 | [diff] [blame] | 556 | if (writeable) { |
| 557 | data = cpu_to_le32(rate); |
| 558 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, |
| 559 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, |
| 560 | UAC2_CS_CONTROL_SAM_FREQ << 8, |
| 561 | snd_usb_ctrl_intf(chip) | (clock << 8), |
| 562 | &data, sizeof(data)); |
| 563 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 564 | usb_audio_err(chip, |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 565 | "%d:%d: cannot set freq %d (v2/v3): err %d\n", |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 566 | iface, fmt->altsetting, rate, err); |
Eldad Zack | 1dc669f | 2013-04-03 23:18:58 +0200 | [diff] [blame] | 567 | return err; |
| 568 | } |
| 569 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 570 | cur_rate = get_sample_rate_v2v3(chip, iface, |
| 571 | fmt->altsetting, clock); |
Eldad Zack | 1dc669f | 2013-04-03 23:18:58 +0200 | [diff] [blame] | 572 | } else { |
| 573 | cur_rate = prev_rate; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Torstein Hegge | 690a863 | 2013-03-26 22:10:05 +0100 | [diff] [blame] | 576 | if (cur_rate != rate) { |
Eldad Zack | 1dc669f | 2013-04-03 23:18:58 +0200 | [diff] [blame] | 577 | if (!writeable) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 578 | usb_audio_warn(chip, |
| 579 | "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n", |
| 580 | iface, fmt->altsetting, rate, cur_rate); |
Eldad Zack | 1dc669f | 2013-04-03 23:18:58 +0200 | [diff] [blame] | 581 | return -ENXIO; |
| 582 | } |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 583 | usb_audio_dbg(chip, |
| 584 | "current rate %d is different from the runtime rate %d\n", |
| 585 | cur_rate, rate); |
Torstein Hegge | 690a863 | 2013-03-26 22:10:05 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* Some devices doesn't respond to sample rate changes while the |
| 589 | * interface is active. */ |
| 590 | if (rate != prev_rate) { |
| 591 | usb_set_interface(dev, iface, 0); |
Daniel Mack | 21bb5aa | 2013-04-10 00:56:03 +0800 | [diff] [blame] | 592 | snd_usb_set_interface_quirk(dev); |
Torstein Hegge | 690a863 | 2013-03-26 22:10:05 +0100 | [diff] [blame] | 593 | usb_set_interface(dev, iface, fmt->altsetting); |
Daniel Mack | 21bb5aa | 2013-04-10 00:56:03 +0800 | [diff] [blame] | 594 | snd_usb_set_interface_quirk(dev); |
Torstein Hegge | 690a863 | 2013-03-26 22:10:05 +0100 | [diff] [blame] | 595 | } |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface, |
| 601 | struct usb_host_interface *alts, |
| 602 | struct audioformat *fmt, int rate) |
| 603 | { |
Clemens Ladisch | 8f898e9 | 2013-01-31 21:39:17 +0100 | [diff] [blame] | 604 | switch (fmt->protocol) { |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 605 | case UAC_VERSION_1: |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 606 | default: |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 607 | return set_sample_rate_v1(chip, iface, alts, fmt, rate); |
| 608 | |
| 609 | case UAC_VERSION_2: |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame^] | 610 | case UAC_VERSION_3: |
| 611 | return set_sample_rate_v2v3(chip, iface, alts, fmt, rate); |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 612 | } |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 613 | } |
| 614 | |