]>
Commit | Line | Data |
---|---|---|
e5779998 DM |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2 of the License, or | |
5 | * (at your option) any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
15 | */ | |
16 | ||
17 | #include <linux/init.h> | |
9966ddaf | 18 | #include <linux/slab.h> |
e5779998 DM |
19 | #include <linux/usb.h> |
20 | #include <linux/usb/audio.h> | |
7e847894 | 21 | #include <linux/usb/audio-v2.h> |
e5779998 DM |
22 | |
23 | #include <sound/core.h> | |
24 | #include <sound/pcm.h> | |
25 | #include <sound/pcm_params.h> | |
26 | ||
27 | #include "usbaudio.h" | |
28 | #include "card.h" | |
29 | #include "quirks.h" | |
30 | #include "debug.h" | |
31 | #include "urb.h" | |
32 | #include "helper.h" | |
33 | #include "pcm.h" | |
79f920fb | 34 | #include "clock.h" |
e5779998 DM |
35 | |
36 | /* | |
37 | * return the current pcm pointer. just based on the hwptr_done value. | |
38 | */ | |
39 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) | |
40 | { | |
41 | struct snd_usb_substream *subs; | |
42 | unsigned int hwptr_done; | |
43 | ||
44 | subs = (struct snd_usb_substream *)substream->runtime->private_data; | |
45 | spin_lock(&subs->lock); | |
46 | hwptr_done = subs->hwptr_done; | |
47 | spin_unlock(&subs->lock); | |
48 | return hwptr_done / (substream->runtime->frame_bits >> 3); | |
49 | } | |
50 | ||
51 | /* | |
52 | * find a matching audio format | |
53 | */ | |
54 | static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format, | |
55 | unsigned int rate, unsigned int channels) | |
56 | { | |
57 | struct list_head *p; | |
58 | struct audioformat *found = NULL; | |
59 | int cur_attr = 0, attr; | |
60 | ||
61 | list_for_each(p, &subs->fmt_list) { | |
62 | struct audioformat *fp; | |
63 | fp = list_entry(p, struct audioformat, list); | |
015eb0b0 CL |
64 | if (!(fp->formats & (1uLL << format))) |
65 | continue; | |
66 | if (fp->channels != channels) | |
e5779998 DM |
67 | continue; |
68 | if (rate < fp->rate_min || rate > fp->rate_max) | |
69 | continue; | |
70 | if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { | |
71 | unsigned int i; | |
72 | for (i = 0; i < fp->nr_rates; i++) | |
73 | if (fp->rate_table[i] == rate) | |
74 | break; | |
75 | if (i >= fp->nr_rates) | |
76 | continue; | |
77 | } | |
78 | attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
79 | if (! found) { | |
80 | found = fp; | |
81 | cur_attr = attr; | |
82 | continue; | |
83 | } | |
84 | /* avoid async out and adaptive in if the other method | |
85 | * supports the same format. | |
86 | * this is a workaround for the case like | |
87 | * M-audio audiophile USB. | |
88 | */ | |
89 | if (attr != cur_attr) { | |
90 | if ((attr == USB_ENDPOINT_SYNC_ASYNC && | |
91 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
92 | (attr == USB_ENDPOINT_SYNC_ADAPTIVE && | |
93 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) | |
94 | continue; | |
95 | if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC && | |
96 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
97 | (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE && | |
98 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { | |
99 | found = fp; | |
100 | cur_attr = attr; | |
101 | continue; | |
102 | } | |
103 | } | |
104 | /* find the format with the largest max. packet size */ | |
105 | if (fp->maxpacksize > found->maxpacksize) { | |
106 | found = fp; | |
107 | cur_attr = attr; | |
108 | } | |
109 | } | |
110 | return found; | |
111 | } | |
112 | ||
767d75ad DM |
113 | static int init_pitch_v1(struct snd_usb_audio *chip, int iface, |
114 | struct usb_host_interface *alts, | |
115 | struct audioformat *fmt) | |
116 | { | |
117 | struct usb_device *dev = chip->dev; | |
118 | unsigned int ep; | |
119 | unsigned char data[1]; | |
120 | int err; | |
121 | ||
122 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
123 | ||
767d75ad DM |
124 | data[0] = 1; |
125 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, | |
126 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | |
127 | UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, | |
128 | data, sizeof(data), 1000)) < 0) { | |
129 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", | |
130 | dev->devnum, iface, ep); | |
131 | return err; | |
132 | } | |
133 | ||
134 | return 0; | |
135 | } | |
e5779998 | 136 | |
92c25611 DM |
137 | static int init_pitch_v2(struct snd_usb_audio *chip, int iface, |
138 | struct usb_host_interface *alts, | |
139 | struct audioformat *fmt) | |
140 | { | |
141 | struct usb_device *dev = chip->dev; | |
142 | unsigned char data[1]; | |
143 | unsigned int ep; | |
144 | int err; | |
145 | ||
146 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
147 | ||
148 | data[0] = 1; | |
149 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, | |
150 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, | |
151 | UAC2_EP_CS_PITCH << 8, 0, | |
152 | data, sizeof(data), 1000)) < 0) { | |
153 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n", | |
154 | dev->devnum, iface, fmt->altsetting); | |
155 | return err; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
e5779998 | 161 | /* |
92c25611 | 162 | * initialize the pitch control and sample rate |
e5779998 | 163 | */ |
767d75ad | 164 | int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface, |
e5779998 DM |
165 | struct usb_host_interface *alts, |
166 | struct audioformat *fmt) | |
167 | { | |
767d75ad DM |
168 | struct usb_interface_descriptor *altsd = get_iface_desc(alts); |
169 | ||
92c25611 DM |
170 | /* if endpoint doesn't have pitch control, bail out */ |
171 | if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL)) | |
172 | return 0; | |
173 | ||
767d75ad DM |
174 | switch (altsd->bInterfaceProtocol) { |
175 | case UAC_VERSION_1: | |
a2acad82 | 176 | default: |
767d75ad DM |
177 | return init_pitch_v1(chip, iface, alts, fmt); |
178 | ||
179 | case UAC_VERSION_2: | |
92c25611 | 180 | return init_pitch_v2(chip, iface, alts, fmt); |
767d75ad | 181 | } |
767d75ad DM |
182 | } |
183 | ||
e5779998 DM |
184 | /* |
185 | * find a matching format and set up the interface | |
186 | */ | |
187 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) | |
188 | { | |
189 | struct usb_device *dev = subs->dev; | |
190 | struct usb_host_interface *alts; | |
191 | struct usb_interface_descriptor *altsd; | |
192 | struct usb_interface *iface; | |
193 | unsigned int ep, attr; | |
194 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; | |
195 | int err; | |
196 | ||
197 | iface = usb_ifnum_to_if(dev, fmt->iface); | |
198 | if (WARN_ON(!iface)) | |
199 | return -EINVAL; | |
200 | alts = &iface->altsetting[fmt->altset_idx]; | |
201 | altsd = get_iface_desc(alts); | |
202 | if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting)) | |
203 | return -EINVAL; | |
204 | ||
205 | if (fmt == subs->cur_audiofmt) | |
206 | return 0; | |
207 | ||
208 | /* close the old interface */ | |
209 | if (subs->interface >= 0 && subs->interface != fmt->iface) { | |
210 | if (usb_set_interface(subs->dev, subs->interface, 0) < 0) { | |
211 | snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n", | |
212 | dev->devnum, fmt->iface, fmt->altsetting); | |
213 | return -EIO; | |
214 | } | |
215 | subs->interface = -1; | |
e11b4e0e | 216 | subs->altset_idx = 0; |
e5779998 DM |
217 | } |
218 | ||
219 | /* set interface */ | |
e11b4e0e | 220 | if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) { |
e5779998 DM |
221 | if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) { |
222 | snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n", | |
223 | dev->devnum, fmt->iface, fmt->altsetting); | |
224 | return -EIO; | |
225 | } | |
226 | snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting); | |
227 | subs->interface = fmt->iface; | |
e11b4e0e | 228 | subs->altset_idx = fmt->altset_idx; |
e5779998 DM |
229 | } |
230 | ||
231 | /* create a data pipe */ | |
232 | ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK; | |
233 | if (is_playback) | |
234 | subs->datapipe = usb_sndisocpipe(dev, ep); | |
235 | else | |
236 | subs->datapipe = usb_rcvisocpipe(dev, ep); | |
237 | subs->datainterval = fmt->datainterval; | |
238 | subs->syncpipe = subs->syncinterval = 0; | |
239 | subs->maxpacksize = fmt->maxpacksize; | |
240 | subs->fill_max = 0; | |
241 | ||
242 | /* we need a sync pipe in async OUT or adaptive IN mode */ | |
243 | /* check the number of EP, since some devices have broken | |
244 | * descriptors which fool us. if it has only one EP, | |
245 | * assume it as adaptive-out or sync-in. | |
246 | */ | |
247 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
248 | if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) || | |
249 | (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) && | |
250 | altsd->bNumEndpoints >= 2) { | |
251 | /* check sync-pipe endpoint */ | |
252 | /* ... and check descriptor size before accessing bSynchAddress | |
253 | because there is a version of the SB Audigy 2 NX firmware lacking | |
254 | the audio fields in the endpoint descriptors */ | |
255 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || | |
256 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
257 | get_endpoint(alts, 1)->bSynchAddress != 0)) { | |
258 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", | |
259 | dev->devnum, fmt->iface, fmt->altsetting); | |
260 | return -EINVAL; | |
261 | } | |
262 | ep = get_endpoint(alts, 1)->bEndpointAddress; | |
263 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
264 | (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || | |
265 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { | |
266 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", | |
267 | dev->devnum, fmt->iface, fmt->altsetting); | |
268 | return -EINVAL; | |
269 | } | |
270 | ep &= USB_ENDPOINT_NUMBER_MASK; | |
271 | if (is_playback) | |
272 | subs->syncpipe = usb_rcvisocpipe(dev, ep); | |
273 | else | |
274 | subs->syncpipe = usb_sndisocpipe(dev, ep); | |
275 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
276 | get_endpoint(alts, 1)->bRefresh >= 1 && | |
277 | get_endpoint(alts, 1)->bRefresh <= 9) | |
278 | subs->syncinterval = get_endpoint(alts, 1)->bRefresh; | |
279 | else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) | |
280 | subs->syncinterval = 1; | |
281 | else if (get_endpoint(alts, 1)->bInterval >= 1 && | |
282 | get_endpoint(alts, 1)->bInterval <= 16) | |
283 | subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1; | |
284 | else | |
285 | subs->syncinterval = 3; | |
286 | } | |
287 | ||
288 | /* always fill max packet size */ | |
289 | if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX) | |
290 | subs->fill_max = 1; | |
291 | ||
767d75ad | 292 | if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0) |
e5779998 DM |
293 | return err; |
294 | ||
295 | subs->cur_audiofmt = fmt; | |
296 | ||
297 | snd_usb_set_format_quirk(subs, fmt); | |
298 | ||
299 | #if 0 | |
300 | printk(KERN_DEBUG | |
301 | "setting done: format = %d, rate = %d..%d, channels = %d\n", | |
302 | fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); | |
303 | printk(KERN_DEBUG | |
304 | " datapipe = 0x%0x, syncpipe = 0x%0x\n", | |
305 | subs->datapipe, subs->syncpipe); | |
306 | #endif | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | /* | |
312 | * hw_params callback | |
313 | * | |
314 | * allocate a buffer and set the given audio format. | |
315 | * | |
316 | * so far we use a physically linear buffer although packetize transfer | |
317 | * doesn't need a continuous area. | |
318 | * if sg buffer is supported on the later version of alsa, we'll follow | |
319 | * that. | |
320 | */ | |
321 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, | |
322 | struct snd_pcm_hw_params *hw_params) | |
323 | { | |
324 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
325 | struct audioformat *fmt; | |
326 | unsigned int channels, rate, format; | |
327 | int ret, changed; | |
328 | ||
329 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
330 | params_buffer_bytes(hw_params)); | |
331 | if (ret < 0) | |
332 | return ret; | |
333 | ||
334 | format = params_format(hw_params); | |
335 | rate = params_rate(hw_params); | |
336 | channels = params_channels(hw_params); | |
337 | fmt = find_format(subs, format, rate, channels); | |
338 | if (!fmt) { | |
339 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", | |
340 | format, rate, channels); | |
341 | return -EINVAL; | |
342 | } | |
343 | ||
344 | changed = subs->cur_audiofmt != fmt || | |
345 | subs->period_bytes != params_period_bytes(hw_params) || | |
346 | subs->cur_rate != rate; | |
347 | if ((ret = set_format(subs, fmt)) < 0) | |
348 | return ret; | |
349 | ||
350 | if (subs->cur_rate != rate) { | |
351 | struct usb_host_interface *alts; | |
352 | struct usb_interface *iface; | |
353 | iface = usb_ifnum_to_if(subs->dev, fmt->iface); | |
354 | alts = &iface->altsetting[fmt->altset_idx]; | |
767d75ad | 355 | ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate); |
e5779998 DM |
356 | if (ret < 0) |
357 | return ret; | |
358 | subs->cur_rate = rate; | |
359 | } | |
360 | ||
361 | if (changed) { | |
362 | /* format changed */ | |
363 | snd_usb_release_substream_urbs(subs, 0); | |
364 | /* influenced: period_bytes, channels, rate, format, */ | |
365 | ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params), | |
366 | params_rate(hw_params), | |
367 | snd_pcm_format_physical_width(params_format(hw_params)) * | |
368 | params_channels(hw_params)); | |
369 | } | |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
374 | /* | |
375 | * hw_free callback | |
376 | * | |
377 | * reset the audio format and release the buffer | |
378 | */ | |
379 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) | |
380 | { | |
381 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
382 | ||
383 | subs->cur_audiofmt = NULL; | |
384 | subs->cur_rate = 0; | |
385 | subs->period_bytes = 0; | |
386 | if (!subs->stream->chip->shutdown) | |
387 | snd_usb_release_substream_urbs(subs, 0); | |
388 | return snd_pcm_lib_free_vmalloc_buffer(substream); | |
389 | } | |
390 | ||
391 | /* | |
392 | * prepare callback | |
393 | * | |
394 | * only a few subtle things... | |
395 | */ | |
396 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) | |
397 | { | |
398 | struct snd_pcm_runtime *runtime = substream->runtime; | |
399 | struct snd_usb_substream *subs = runtime->private_data; | |
400 | ||
401 | if (! subs->cur_audiofmt) { | |
402 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); | |
403 | return -ENXIO; | |
404 | } | |
405 | ||
406 | /* some unit conversions in runtime */ | |
407 | subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize); | |
408 | subs->curframesize = bytes_to_frames(runtime, subs->curpacksize); | |
409 | ||
410 | /* reset the pointer */ | |
411 | subs->hwptr_done = 0; | |
412 | subs->transfer_done = 0; | |
413 | subs->phase = 0; | |
414 | runtime->delay = 0; | |
415 | ||
416 | return snd_usb_substream_prepare(subs, runtime); | |
417 | } | |
418 | ||
419 | static struct snd_pcm_hardware snd_usb_hardware = | |
420 | { | |
421 | .info = SNDRV_PCM_INFO_MMAP | | |
422 | SNDRV_PCM_INFO_MMAP_VALID | | |
423 | SNDRV_PCM_INFO_BATCH | | |
424 | SNDRV_PCM_INFO_INTERLEAVED | | |
425 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
426 | SNDRV_PCM_INFO_PAUSE, | |
427 | .buffer_bytes_max = 1024 * 1024, | |
428 | .period_bytes_min = 64, | |
429 | .period_bytes_max = 512 * 1024, | |
430 | .periods_min = 2, | |
431 | .periods_max = 1024, | |
432 | }; | |
433 | ||
434 | static int hw_check_valid_format(struct snd_usb_substream *subs, | |
435 | struct snd_pcm_hw_params *params, | |
436 | struct audioformat *fp) | |
437 | { | |
438 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
439 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
440 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
441 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
015eb0b0 | 442 | struct snd_mask check_fmts; |
e5779998 DM |
443 | unsigned int ptime; |
444 | ||
445 | /* check the format */ | |
015eb0b0 CL |
446 | snd_mask_none(&check_fmts); |
447 | check_fmts.bits[0] = (u32)fp->formats; | |
448 | check_fmts.bits[1] = (u32)(fp->formats >> 32); | |
449 | snd_mask_intersect(&check_fmts, fmts); | |
450 | if (snd_mask_empty(&check_fmts)) { | |
e5779998 DM |
451 | hwc_debug(" > check: no supported format %d\n", fp->format); |
452 | return 0; | |
453 | } | |
454 | /* check the channels */ | |
455 | if (fp->channels < ct->min || fp->channels > ct->max) { | |
456 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); | |
457 | return 0; | |
458 | } | |
459 | /* check the rate is within the range */ | |
460 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { | |
461 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); | |
462 | return 0; | |
463 | } | |
464 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { | |
465 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); | |
466 | return 0; | |
467 | } | |
468 | /* check whether the period time is >= the data packet interval */ | |
469 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) { | |
470 | ptime = 125 * (1 << fp->datainterval); | |
471 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { | |
472 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); | |
473 | return 0; | |
474 | } | |
475 | } | |
476 | return 1; | |
477 | } | |
478 | ||
479 | static int hw_rule_rate(struct snd_pcm_hw_params *params, | |
480 | struct snd_pcm_hw_rule *rule) | |
481 | { | |
482 | struct snd_usb_substream *subs = rule->private; | |
483 | struct list_head *p; | |
484 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
485 | unsigned int rmin, rmax; | |
486 | int changed; | |
487 | ||
488 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); | |
489 | changed = 0; | |
490 | rmin = rmax = 0; | |
491 | list_for_each(p, &subs->fmt_list) { | |
492 | struct audioformat *fp; | |
493 | fp = list_entry(p, struct audioformat, list); | |
494 | if (!hw_check_valid_format(subs, params, fp)) | |
495 | continue; | |
496 | if (changed++) { | |
497 | if (rmin > fp->rate_min) | |
498 | rmin = fp->rate_min; | |
499 | if (rmax < fp->rate_max) | |
500 | rmax = fp->rate_max; | |
501 | } else { | |
502 | rmin = fp->rate_min; | |
503 | rmax = fp->rate_max; | |
504 | } | |
505 | } | |
506 | ||
507 | if (!changed) { | |
508 | hwc_debug(" --> get empty\n"); | |
509 | it->empty = 1; | |
510 | return -EINVAL; | |
511 | } | |
512 | ||
513 | changed = 0; | |
514 | if (it->min < rmin) { | |
515 | it->min = rmin; | |
516 | it->openmin = 0; | |
517 | changed = 1; | |
518 | } | |
519 | if (it->max > rmax) { | |
520 | it->max = rmax; | |
521 | it->openmax = 0; | |
522 | changed = 1; | |
523 | } | |
524 | if (snd_interval_checkempty(it)) { | |
525 | it->empty = 1; | |
526 | return -EINVAL; | |
527 | } | |
528 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
529 | return changed; | |
530 | } | |
531 | ||
532 | ||
533 | static int hw_rule_channels(struct snd_pcm_hw_params *params, | |
534 | struct snd_pcm_hw_rule *rule) | |
535 | { | |
536 | struct snd_usb_substream *subs = rule->private; | |
537 | struct list_head *p; | |
538 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
539 | unsigned int rmin, rmax; | |
540 | int changed; | |
541 | ||
542 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); | |
543 | changed = 0; | |
544 | rmin = rmax = 0; | |
545 | list_for_each(p, &subs->fmt_list) { | |
546 | struct audioformat *fp; | |
547 | fp = list_entry(p, struct audioformat, list); | |
548 | if (!hw_check_valid_format(subs, params, fp)) | |
549 | continue; | |
550 | if (changed++) { | |
551 | if (rmin > fp->channels) | |
552 | rmin = fp->channels; | |
553 | if (rmax < fp->channels) | |
554 | rmax = fp->channels; | |
555 | } else { | |
556 | rmin = fp->channels; | |
557 | rmax = fp->channels; | |
558 | } | |
559 | } | |
560 | ||
561 | if (!changed) { | |
562 | hwc_debug(" --> get empty\n"); | |
563 | it->empty = 1; | |
564 | return -EINVAL; | |
565 | } | |
566 | ||
567 | changed = 0; | |
568 | if (it->min < rmin) { | |
569 | it->min = rmin; | |
570 | it->openmin = 0; | |
571 | changed = 1; | |
572 | } | |
573 | if (it->max > rmax) { | |
574 | it->max = rmax; | |
575 | it->openmax = 0; | |
576 | changed = 1; | |
577 | } | |
578 | if (snd_interval_checkempty(it)) { | |
579 | it->empty = 1; | |
580 | return -EINVAL; | |
581 | } | |
582 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
583 | return changed; | |
584 | } | |
585 | ||
586 | static int hw_rule_format(struct snd_pcm_hw_params *params, | |
587 | struct snd_pcm_hw_rule *rule) | |
588 | { | |
589 | struct snd_usb_substream *subs = rule->private; | |
590 | struct list_head *p; | |
591 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
592 | u64 fbits; | |
593 | u32 oldbits[2]; | |
594 | int changed; | |
595 | ||
596 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); | |
597 | fbits = 0; | |
598 | list_for_each(p, &subs->fmt_list) { | |
599 | struct audioformat *fp; | |
600 | fp = list_entry(p, struct audioformat, list); | |
601 | if (!hw_check_valid_format(subs, params, fp)) | |
602 | continue; | |
015eb0b0 | 603 | fbits |= fp->formats; |
e5779998 DM |
604 | } |
605 | ||
606 | oldbits[0] = fmt->bits[0]; | |
607 | oldbits[1] = fmt->bits[1]; | |
608 | fmt->bits[0] &= (u32)fbits; | |
609 | fmt->bits[1] &= (u32)(fbits >> 32); | |
610 | if (!fmt->bits[0] && !fmt->bits[1]) { | |
611 | hwc_debug(" --> get empty\n"); | |
612 | return -EINVAL; | |
613 | } | |
614 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); | |
615 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); | |
616 | return changed; | |
617 | } | |
618 | ||
619 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, | |
620 | struct snd_pcm_hw_rule *rule) | |
621 | { | |
622 | struct snd_usb_substream *subs = rule->private; | |
623 | struct audioformat *fp; | |
624 | struct snd_interval *it; | |
625 | unsigned char min_datainterval; | |
626 | unsigned int pmin; | |
627 | int changed; | |
628 | ||
629 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
630 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); | |
631 | min_datainterval = 0xff; | |
632 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
633 | if (!hw_check_valid_format(subs, params, fp)) | |
634 | continue; | |
635 | min_datainterval = min(min_datainterval, fp->datainterval); | |
636 | } | |
637 | if (min_datainterval == 0xff) { | |
a7ce2e0d | 638 | hwc_debug(" --> get empty\n"); |
e5779998 DM |
639 | it->empty = 1; |
640 | return -EINVAL; | |
641 | } | |
642 | pmin = 125 * (1 << min_datainterval); | |
643 | changed = 0; | |
644 | if (it->min < pmin) { | |
645 | it->min = pmin; | |
646 | it->openmin = 0; | |
647 | changed = 1; | |
648 | } | |
649 | if (snd_interval_checkempty(it)) { | |
650 | it->empty = 1; | |
651 | return -EINVAL; | |
652 | } | |
653 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); | |
654 | return changed; | |
655 | } | |
656 | ||
657 | /* | |
658 | * If the device supports unusual bit rates, does the request meet these? | |
659 | */ | |
660 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, | |
661 | struct snd_usb_substream *subs) | |
662 | { | |
663 | struct audioformat *fp; | |
664 | int count = 0, needs_knot = 0; | |
665 | int err; | |
666 | ||
667 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
668 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) | |
669 | return 0; | |
670 | count += fp->nr_rates; | |
671 | if (fp->rates & SNDRV_PCM_RATE_KNOT) | |
672 | needs_knot = 1; | |
673 | } | |
674 | if (!needs_knot) | |
675 | return 0; | |
676 | ||
677 | subs->rate_list.count = count; | |
678 | subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL); | |
679 | subs->rate_list.mask = 0; | |
680 | count = 0; | |
681 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
682 | int i; | |
683 | for (i = 0; i < fp->nr_rates; i++) | |
684 | subs->rate_list.list[count++] = fp->rate_table[i]; | |
685 | } | |
686 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
687 | &subs->rate_list); | |
688 | if (err < 0) | |
689 | return err; | |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
694 | ||
695 | /* | |
696 | * set up the runtime hardware information. | |
697 | */ | |
698 | ||
699 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) | |
700 | { | |
701 | struct list_head *p; | |
702 | unsigned int pt, ptmin; | |
703 | int param_period_time_if_needed; | |
704 | int err; | |
705 | ||
706 | runtime->hw.formats = subs->formats; | |
707 | ||
708 | runtime->hw.rate_min = 0x7fffffff; | |
709 | runtime->hw.rate_max = 0; | |
710 | runtime->hw.channels_min = 256; | |
711 | runtime->hw.channels_max = 0; | |
712 | runtime->hw.rates = 0; | |
713 | ptmin = UINT_MAX; | |
714 | /* check min/max rates and channels */ | |
715 | list_for_each(p, &subs->fmt_list) { | |
716 | struct audioformat *fp; | |
717 | fp = list_entry(p, struct audioformat, list); | |
718 | runtime->hw.rates |= fp->rates; | |
719 | if (runtime->hw.rate_min > fp->rate_min) | |
720 | runtime->hw.rate_min = fp->rate_min; | |
721 | if (runtime->hw.rate_max < fp->rate_max) | |
722 | runtime->hw.rate_max = fp->rate_max; | |
723 | if (runtime->hw.channels_min > fp->channels) | |
724 | runtime->hw.channels_min = fp->channels; | |
725 | if (runtime->hw.channels_max < fp->channels) | |
726 | runtime->hw.channels_max = fp->channels; | |
727 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { | |
728 | /* FIXME: there might be more than one audio formats... */ | |
729 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = | |
730 | fp->frame_size; | |
731 | } | |
732 | pt = 125 * (1 << fp->datainterval); | |
733 | ptmin = min(ptmin, pt); | |
734 | } | |
735 | ||
736 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; | |
737 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH) | |
738 | /* full speed devices have fixed data packet interval */ | |
739 | ptmin = 1000; | |
740 | if (ptmin == 1000) | |
741 | /* if period time doesn't go below 1 ms, no rules needed */ | |
742 | param_period_time_if_needed = -1; | |
743 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
744 | ptmin, UINT_MAX); | |
745 | ||
746 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
747 | hw_rule_rate, subs, | |
748 | SNDRV_PCM_HW_PARAM_FORMAT, | |
749 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
750 | param_period_time_if_needed, | |
751 | -1)) < 0) | |
752 | return err; | |
753 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, | |
754 | hw_rule_channels, subs, | |
755 | SNDRV_PCM_HW_PARAM_FORMAT, | |
756 | SNDRV_PCM_HW_PARAM_RATE, | |
757 | param_period_time_if_needed, | |
758 | -1)) < 0) | |
759 | return err; | |
760 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, | |
761 | hw_rule_format, subs, | |
762 | SNDRV_PCM_HW_PARAM_RATE, | |
763 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
764 | param_period_time_if_needed, | |
765 | -1)) < 0) | |
766 | return err; | |
767 | if (param_period_time_if_needed >= 0) { | |
768 | err = snd_pcm_hw_rule_add(runtime, 0, | |
769 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
770 | hw_rule_period_time, subs, | |
771 | SNDRV_PCM_HW_PARAM_FORMAT, | |
772 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
773 | SNDRV_PCM_HW_PARAM_RATE, | |
774 | -1); | |
775 | if (err < 0) | |
776 | return err; | |
777 | } | |
778 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) | |
779 | return err; | |
780 | return 0; | |
781 | } | |
782 | ||
783 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) | |
784 | { | |
785 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
786 | struct snd_pcm_runtime *runtime = substream->runtime; | |
787 | struct snd_usb_substream *subs = &as->substream[direction]; | |
788 | ||
789 | subs->interface = -1; | |
e11b4e0e | 790 | subs->altset_idx = 0; |
e5779998 DM |
791 | runtime->hw = snd_usb_hardware; |
792 | runtime->private_data = subs; | |
793 | subs->pcm_substream = substream; | |
794 | return setup_hw_info(runtime, subs); | |
795 | } | |
796 | ||
797 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) | |
798 | { | |
799 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
800 | struct snd_usb_substream *subs = &as->substream[direction]; | |
801 | ||
802 | if (!as->chip->shutdown && subs->interface >= 0) { | |
803 | usb_set_interface(subs->dev, subs->interface, 0); | |
804 | subs->interface = -1; | |
805 | } | |
806 | subs->pcm_substream = NULL; | |
807 | return 0; | |
808 | } | |
809 | ||
810 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) | |
811 | { | |
812 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
813 | } | |
814 | ||
815 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) | |
816 | { | |
817 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
818 | } | |
819 | ||
820 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) | |
821 | { | |
822 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); | |
823 | } | |
824 | ||
825 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) | |
826 | { | |
827 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); | |
828 | } | |
829 | ||
830 | static struct snd_pcm_ops snd_usb_playback_ops = { | |
831 | .open = snd_usb_playback_open, | |
832 | .close = snd_usb_playback_close, | |
833 | .ioctl = snd_pcm_lib_ioctl, | |
834 | .hw_params = snd_usb_hw_params, | |
835 | .hw_free = snd_usb_hw_free, | |
836 | .prepare = snd_usb_pcm_prepare, | |
837 | .trigger = snd_usb_substream_playback_trigger, | |
838 | .pointer = snd_usb_pcm_pointer, | |
839 | .page = snd_pcm_lib_get_vmalloc_page, | |
840 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
841 | }; | |
842 | ||
843 | static struct snd_pcm_ops snd_usb_capture_ops = { | |
844 | .open = snd_usb_capture_open, | |
845 | .close = snd_usb_capture_close, | |
846 | .ioctl = snd_pcm_lib_ioctl, | |
847 | .hw_params = snd_usb_hw_params, | |
848 | .hw_free = snd_usb_hw_free, | |
849 | .prepare = snd_usb_pcm_prepare, | |
850 | .trigger = snd_usb_substream_capture_trigger, | |
851 | .pointer = snd_usb_pcm_pointer, | |
852 | .page = snd_pcm_lib_get_vmalloc_page, | |
853 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
854 | }; | |
855 | ||
856 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) | |
857 | { | |
858 | snd_pcm_set_ops(pcm, stream, | |
859 | stream == SNDRV_PCM_STREAM_PLAYBACK ? | |
860 | &snd_usb_playback_ops : &snd_usb_capture_ops); | |
861 | } |