ALSA: usb-audio: UAC2: support read-only freq control
[linux-2.6-block.git] / sound / usb / clock.c
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>
22 #include <linux/string.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 #include <linux/usb/audio-v2.h>
26
27 #include <sound/core.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30
31 #include "usbaudio.h"
32 #include "card.h"
33 #include "helper.h"
34 #include "clock.h"
35
36 static struct uac_clock_source_descriptor *
37         snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
38                                   int clock_id)
39 {
40         struct uac_clock_source_descriptor *cs = NULL;
41
42         while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
43                                              ctrl_iface->extralen,
44                                              cs, UAC2_CLOCK_SOURCE))) {
45                 if (cs->bClockID == clock_id)
46                         return cs;
47         }
48
49         return NULL;
50 }
51
52 static struct uac_clock_selector_descriptor *
53         snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
54                                     int clock_id)
55 {
56         struct uac_clock_selector_descriptor *cs = NULL;
57
58         while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
59                                              ctrl_iface->extralen,
60                                              cs, UAC2_CLOCK_SELECTOR))) {
61                 if (cs->bClockID == clock_id)
62                         return cs;
63         }
64
65         return NULL;
66 }
67
68 static struct uac_clock_multiplier_descriptor *
69         snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
70                                       int clock_id)
71 {
72         struct uac_clock_multiplier_descriptor *cs = NULL;
73
74         while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
75                                              ctrl_iface->extralen,
76                                              cs, UAC2_CLOCK_MULTIPLIER))) {
77                 if (cs->bClockID == clock_id)
78                         return cs;
79         }
80
81         return NULL;
82 }
83
84 static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
85 {
86         unsigned char buf;
87         int ret;
88
89         ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
90                               UAC2_CS_CUR,
91                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
92                               UAC2_CX_CLOCK_SELECTOR << 8,
93                               snd_usb_ctrl_intf(chip) | (selector_id << 8),
94                               &buf, sizeof(buf));
95
96         if (ret < 0)
97                 return ret;
98
99         return buf;
100 }
101
102 static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
103                                         unsigned char pin)
104 {
105         int ret;
106
107         ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
108                               UAC2_CS_CUR,
109                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
110                               UAC2_CX_CLOCK_SELECTOR << 8,
111                               snd_usb_ctrl_intf(chip) | (selector_id << 8),
112                               &pin, sizeof(pin));
113         if (ret < 0)
114                 return ret;
115
116         if (ret != sizeof(pin)) {
117                 snd_printk(KERN_ERR
118                         "usb-audio:%d: setting selector (id %d) unexpected length %d\n",
119                         chip->dev->devnum, selector_id, ret);
120                 return -EINVAL;
121         }
122
123         ret = uac_clock_selector_get_val(chip, selector_id);
124         if (ret < 0)
125                 return ret;
126
127         if (ret != pin) {
128                 snd_printk(KERN_ERR
129                         "usb-audio:%d: setting selector (id %d) to %x failed (current: %d)\n",
130                         chip->dev->devnum, selector_id, pin, ret);
131                 return -EINVAL;
132         }
133
134         return ret;
135 }
136
137 static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
138 {
139         int err;
140         unsigned char data;
141         struct usb_device *dev = chip->dev;
142         struct uac_clock_source_descriptor *cs_desc =
143                 snd_usb_find_clock_source(chip->ctrl_intf, source_id);
144
145         if (!cs_desc)
146                 return 0;
147
148         /* If a clock source can't tell us whether it's valid, we assume it is */
149         if (!uac2_control_is_readable(cs_desc->bmControls,
150                                       UAC2_CS_CONTROL_CLOCK_VALID - 1))
151                 return 1;
152
153         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
154                               USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
155                               UAC2_CS_CONTROL_CLOCK_VALID << 8,
156                               snd_usb_ctrl_intf(chip) | (source_id << 8),
157                               &data, sizeof(data));
158
159         if (err < 0) {
160                 snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
161                            __func__, source_id);
162                 return 0;
163         }
164
165         return !!data;
166 }
167
168 static int __uac_clock_find_source(struct snd_usb_audio *chip,
169                                    int entity_id, unsigned long *visited,
170                                    bool validate)
171 {
172         struct uac_clock_source_descriptor *source;
173         struct uac_clock_selector_descriptor *selector;
174         struct uac_clock_multiplier_descriptor *multiplier;
175
176         entity_id &= 0xff;
177
178         if (test_and_set_bit(entity_id, visited)) {
179                 snd_printk(KERN_WARNING
180                         "%s(): recursive clock topology detected, id %d.\n",
181                         __func__, entity_id);
182                 return -EINVAL;
183         }
184
185         /* first, see if the ID we're looking for is a clock source already */
186         source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
187         if (source) {
188                 entity_id = source->bClockID;
189                 if (validate && !uac_clock_source_is_valid(chip, entity_id)) {
190                         snd_printk(KERN_ERR "usb-audio:%d: clock source %d is not valid, cannot use\n",
191                                    chip->dev->devnum, entity_id);
192                         return -ENXIO;
193                 }
194                 return entity_id;
195         }
196
197         selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
198         if (selector) {
199                 int ret, i, cur;
200
201                 /* the entity ID we are looking for is a selector.
202                  * find out what it currently selects */
203                 ret = uac_clock_selector_get_val(chip, selector->bClockID);
204                 if (ret < 0)
205                         return ret;
206
207                 /* Selector values are one-based */
208
209                 if (ret > selector->bNrInPins || ret < 1) {
210                         snd_printk(KERN_ERR
211                                 "%s(): selector reported illegal value, id %d, ret %d\n",
212                                 __func__, selector->bClockID, ret);
213
214                         return -EINVAL;
215                 }
216
217                 cur = ret;
218                 ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
219                                                visited, validate);
220                 if (!validate || ret > 0 || !chip->autoclock)
221                         return ret;
222
223                 /* The current clock source is invalid, try others. */
224                 for (i = 1; i <= selector->bNrInPins; i++) {
225                         int err;
226
227                         if (i == cur)
228                                 continue;
229
230                         ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
231                                 visited, true);
232                         if (ret < 0)
233                                 continue;
234
235                         err = uac_clock_selector_set_val(chip, entity_id, i);
236                         if (err < 0)
237                                 continue;
238
239                         snd_printk(KERN_INFO
240                                 "usb-audio:%d: found and selected valid clock source %d\n",
241                                 chip->dev->devnum, ret);
242                         return ret;
243                 }
244
245                 return -ENXIO;
246         }
247
248         /* FIXME: multipliers only act as pass-thru element for now */
249         multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
250         if (multiplier)
251                 return __uac_clock_find_source(chip, multiplier->bCSourceID,
252                                                 visited, validate);
253
254         return -EINVAL;
255 }
256
257 /*
258  * For all kinds of sample rate settings and other device queries,
259  * the clock source (end-leaf) must be used. However, clock selectors,
260  * clock multipliers and sample rate converters may be specified as
261  * clock source input to terminal. This functions walks the clock path
262  * to its end and tries to find the source.
263  *
264  * The 'visited' bitfield is used internally to detect recursive loops.
265  *
266  * Returns the clock source UnitID (>=0) on success, or an error.
267  */
268 int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
269                               bool validate)
270 {
271         DECLARE_BITMAP(visited, 256);
272         memset(visited, 0, sizeof(visited));
273         return __uac_clock_find_source(chip, entity_id, visited, validate);
274 }
275
276 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
277                               struct usb_host_interface *alts,
278                               struct audioformat *fmt, int rate)
279 {
280         struct usb_device *dev = chip->dev;
281         unsigned int ep;
282         unsigned char data[3];
283         int err, crate;
284
285         ep = get_endpoint(alts, 0)->bEndpointAddress;
286
287         /* if endpoint doesn't have sampling rate control, bail out */
288         if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
289                 return 0;
290
291         data[0] = rate;
292         data[1] = rate >> 8;
293         data[2] = rate >> 16;
294         if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
295                                    USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
296                                    UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
297                                    data, sizeof(data))) < 0) {
298                 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
299                            dev->devnum, iface, fmt->altsetting, rate, ep);
300                 return err;
301         }
302
303         if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
304                                    USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
305                                    UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
306                                    data, sizeof(data))) < 0) {
307                 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
308                            dev->devnum, iface, fmt->altsetting, ep);
309                 return 0; /* some devices don't support reading */
310         }
311
312         crate = data[0] | (data[1] << 8) | (data[2] << 16);
313         if (crate != rate) {
314                 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
315                 // runtime->rate = crate;
316         }
317
318         return 0;
319 }
320
321 static int get_sample_rate_v2(struct snd_usb_audio *chip, int iface,
322                               int altsetting, int clock)
323 {
324         struct usb_device *dev = chip->dev;
325         __le32 data;
326         int err;
327
328         err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
329                               USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
330                               UAC2_CS_CONTROL_SAM_FREQ << 8,
331                               snd_usb_ctrl_intf(chip) | (clock << 8),
332                               &data, sizeof(data));
333         if (err < 0) {
334                 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2): err %d\n",
335                            dev->devnum, iface, altsetting, err);
336                 return 0;
337         }
338
339         return le32_to_cpu(data);
340 }
341
342 static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
343                               struct usb_host_interface *alts,
344                               struct audioformat *fmt, int rate)
345 {
346         struct usb_device *dev = chip->dev;
347         __le32 data;
348         int err, cur_rate, prev_rate;
349         int clock;
350         bool writeable;
351         struct uac_clock_source_descriptor *cs_desc;
352
353         clock = snd_usb_clock_find_source(chip, fmt->clock, true);
354         if (clock < 0)
355                 return clock;
356
357         prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
358
359         cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
360         writeable = uac2_control_is_writeable(cs_desc->bmControls, UAC2_CS_CONTROL_SAM_FREQ - 1);
361         if (writeable) {
362                 data = cpu_to_le32(rate);
363                 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
364                                       USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
365                                       UAC2_CS_CONTROL_SAM_FREQ << 8,
366                                       snd_usb_ctrl_intf(chip) | (clock << 8),
367                                       &data, sizeof(data));
368                 if (err < 0) {
369                         snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2): err %d\n",
370                                    dev->devnum, iface, fmt->altsetting, rate, err);
371                         return err;
372                 }
373
374                 cur_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
375         } else {
376                 cur_rate = prev_rate;
377         }
378
379         if (cur_rate != rate) {
380                 if (!writeable) {
381                         snd_printk(KERN_WARNING
382                                    "%d:%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
383                                    dev->devnum, iface, fmt->altsetting, rate, cur_rate);
384                         return -ENXIO;
385                 }
386                 snd_printd(KERN_WARNING
387                            "current rate %d is different from the runtime rate %d\n",
388                            cur_rate, rate);
389         }
390
391         /* Some devices doesn't respond to sample rate changes while the
392          * interface is active. */
393         if (rate != prev_rate) {
394                 usb_set_interface(dev, iface, 0);
395                 usb_set_interface(dev, iface, fmt->altsetting);
396         }
397
398         return 0;
399 }
400
401 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
402                              struct usb_host_interface *alts,
403                              struct audioformat *fmt, int rate)
404 {
405         struct usb_interface_descriptor *altsd = get_iface_desc(alts);
406
407         switch (altsd->bInterfaceProtocol) {
408         case UAC_VERSION_1:
409         default:
410                 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
411
412         case UAC_VERSION_2:
413                 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
414         }
415 }
416