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; | |
89e1e66d | 240 | subs->syncmaxsize = 0; |
e5779998 DM |
241 | subs->fill_max = 0; |
242 | ||
243 | /* we need a sync pipe in async OUT or adaptive IN mode */ | |
244 | /* check the number of EP, since some devices have broken | |
245 | * descriptors which fool us. if it has only one EP, | |
246 | * assume it as adaptive-out or sync-in. | |
247 | */ | |
248 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
249 | if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) || | |
250 | (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) && | |
251 | altsd->bNumEndpoints >= 2) { | |
252 | /* check sync-pipe endpoint */ | |
253 | /* ... and check descriptor size before accessing bSynchAddress | |
254 | because there is a version of the SB Audigy 2 NX firmware lacking | |
255 | the audio fields in the endpoint descriptors */ | |
256 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || | |
257 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
258 | get_endpoint(alts, 1)->bSynchAddress != 0)) { | |
259 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", | |
260 | dev->devnum, fmt->iface, fmt->altsetting); | |
261 | return -EINVAL; | |
262 | } | |
263 | ep = get_endpoint(alts, 1)->bEndpointAddress; | |
264 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
265 | (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || | |
266 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { | |
267 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", | |
268 | dev->devnum, fmt->iface, fmt->altsetting); | |
269 | return -EINVAL; | |
270 | } | |
271 | ep &= USB_ENDPOINT_NUMBER_MASK; | |
272 | if (is_playback) | |
273 | subs->syncpipe = usb_rcvisocpipe(dev, ep); | |
274 | else | |
275 | subs->syncpipe = usb_sndisocpipe(dev, ep); | |
276 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
277 | get_endpoint(alts, 1)->bRefresh >= 1 && | |
278 | get_endpoint(alts, 1)->bRefresh <= 9) | |
279 | subs->syncinterval = get_endpoint(alts, 1)->bRefresh; | |
280 | else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) | |
281 | subs->syncinterval = 1; | |
282 | else if (get_endpoint(alts, 1)->bInterval >= 1 && | |
283 | get_endpoint(alts, 1)->bInterval <= 16) | |
284 | subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1; | |
285 | else | |
286 | subs->syncinterval = 3; | |
89e1e66d | 287 | subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize); |
e5779998 DM |
288 | } |
289 | ||
290 | /* always fill max packet size */ | |
291 | if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX) | |
292 | subs->fill_max = 1; | |
293 | ||
767d75ad | 294 | if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0) |
e5779998 DM |
295 | return err; |
296 | ||
297 | subs->cur_audiofmt = fmt; | |
298 | ||
299 | snd_usb_set_format_quirk(subs, fmt); | |
300 | ||
301 | #if 0 | |
302 | printk(KERN_DEBUG | |
303 | "setting done: format = %d, rate = %d..%d, channels = %d\n", | |
304 | fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); | |
305 | printk(KERN_DEBUG | |
306 | " datapipe = 0x%0x, syncpipe = 0x%0x\n", | |
307 | subs->datapipe, subs->syncpipe); | |
308 | #endif | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
313 | /* | |
314 | * hw_params callback | |
315 | * | |
316 | * allocate a buffer and set the given audio format. | |
317 | * | |
318 | * so far we use a physically linear buffer although packetize transfer | |
319 | * doesn't need a continuous area. | |
320 | * if sg buffer is supported on the later version of alsa, we'll follow | |
321 | * that. | |
322 | */ | |
323 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, | |
324 | struct snd_pcm_hw_params *hw_params) | |
325 | { | |
326 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
327 | struct audioformat *fmt; | |
328 | unsigned int channels, rate, format; | |
329 | int ret, changed; | |
330 | ||
331 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
332 | params_buffer_bytes(hw_params)); | |
333 | if (ret < 0) | |
334 | return ret; | |
335 | ||
336 | format = params_format(hw_params); | |
337 | rate = params_rate(hw_params); | |
338 | channels = params_channels(hw_params); | |
339 | fmt = find_format(subs, format, rate, channels); | |
340 | if (!fmt) { | |
341 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", | |
342 | format, rate, channels); | |
343 | return -EINVAL; | |
344 | } | |
345 | ||
346 | changed = subs->cur_audiofmt != fmt || | |
347 | subs->period_bytes != params_period_bytes(hw_params) || | |
348 | subs->cur_rate != rate; | |
349 | if ((ret = set_format(subs, fmt)) < 0) | |
350 | return ret; | |
351 | ||
352 | if (subs->cur_rate != rate) { | |
353 | struct usb_host_interface *alts; | |
354 | struct usb_interface *iface; | |
355 | iface = usb_ifnum_to_if(subs->dev, fmt->iface); | |
356 | alts = &iface->altsetting[fmt->altset_idx]; | |
767d75ad | 357 | ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate); |
e5779998 DM |
358 | if (ret < 0) |
359 | return ret; | |
360 | subs->cur_rate = rate; | |
361 | } | |
362 | ||
363 | if (changed) { | |
382225e6 | 364 | mutex_lock(&subs->stream->chip->shutdown_mutex); |
e5779998 DM |
365 | /* format changed */ |
366 | snd_usb_release_substream_urbs(subs, 0); | |
367 | /* influenced: period_bytes, channels, rate, format, */ | |
368 | ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params), | |
369 | params_rate(hw_params), | |
370 | snd_pcm_format_physical_width(params_format(hw_params)) * | |
371 | params_channels(hw_params)); | |
382225e6 | 372 | mutex_unlock(&subs->stream->chip->shutdown_mutex); |
e5779998 DM |
373 | } |
374 | ||
375 | return ret; | |
376 | } | |
377 | ||
378 | /* | |
379 | * hw_free callback | |
380 | * | |
381 | * reset the audio format and release the buffer | |
382 | */ | |
383 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) | |
384 | { | |
385 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
386 | ||
387 | subs->cur_audiofmt = NULL; | |
388 | subs->cur_rate = 0; | |
389 | subs->period_bytes = 0; | |
382225e6 TI |
390 | mutex_lock(&subs->stream->chip->shutdown_mutex); |
391 | snd_usb_release_substream_urbs(subs, 0); | |
392 | mutex_unlock(&subs->stream->chip->shutdown_mutex); | |
e5779998 DM |
393 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
394 | } | |
395 | ||
396 | /* | |
397 | * prepare callback | |
398 | * | |
399 | * only a few subtle things... | |
400 | */ | |
401 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) | |
402 | { | |
403 | struct snd_pcm_runtime *runtime = substream->runtime; | |
404 | struct snd_usb_substream *subs = runtime->private_data; | |
405 | ||
406 | if (! subs->cur_audiofmt) { | |
407 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); | |
408 | return -ENXIO; | |
409 | } | |
410 | ||
411 | /* some unit conversions in runtime */ | |
412 | subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize); | |
413 | subs->curframesize = bytes_to_frames(runtime, subs->curpacksize); | |
414 | ||
415 | /* reset the pointer */ | |
416 | subs->hwptr_done = 0; | |
417 | subs->transfer_done = 0; | |
418 | subs->phase = 0; | |
419 | runtime->delay = 0; | |
420 | ||
421 | return snd_usb_substream_prepare(subs, runtime); | |
422 | } | |
423 | ||
424 | static struct snd_pcm_hardware snd_usb_hardware = | |
425 | { | |
426 | .info = SNDRV_PCM_INFO_MMAP | | |
427 | SNDRV_PCM_INFO_MMAP_VALID | | |
428 | SNDRV_PCM_INFO_BATCH | | |
429 | SNDRV_PCM_INFO_INTERLEAVED | | |
430 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
431 | SNDRV_PCM_INFO_PAUSE, | |
432 | .buffer_bytes_max = 1024 * 1024, | |
433 | .period_bytes_min = 64, | |
434 | .period_bytes_max = 512 * 1024, | |
435 | .periods_min = 2, | |
436 | .periods_max = 1024, | |
437 | }; | |
438 | ||
439 | static int hw_check_valid_format(struct snd_usb_substream *subs, | |
440 | struct snd_pcm_hw_params *params, | |
441 | struct audioformat *fp) | |
442 | { | |
443 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
444 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
445 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
446 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
015eb0b0 | 447 | struct snd_mask check_fmts; |
e5779998 DM |
448 | unsigned int ptime; |
449 | ||
450 | /* check the format */ | |
015eb0b0 CL |
451 | snd_mask_none(&check_fmts); |
452 | check_fmts.bits[0] = (u32)fp->formats; | |
453 | check_fmts.bits[1] = (u32)(fp->formats >> 32); | |
454 | snd_mask_intersect(&check_fmts, fmts); | |
455 | if (snd_mask_empty(&check_fmts)) { | |
e5779998 DM |
456 | hwc_debug(" > check: no supported format %d\n", fp->format); |
457 | return 0; | |
458 | } | |
459 | /* check the channels */ | |
460 | if (fp->channels < ct->min || fp->channels > ct->max) { | |
461 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); | |
462 | return 0; | |
463 | } | |
464 | /* check the rate is within the range */ | |
465 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { | |
466 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); | |
467 | return 0; | |
468 | } | |
469 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { | |
470 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); | |
471 | return 0; | |
472 | } | |
473 | /* check whether the period time is >= the data packet interval */ | |
4f4e8f69 | 474 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) { |
e5779998 DM |
475 | ptime = 125 * (1 << fp->datainterval); |
476 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { | |
477 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); | |
478 | return 0; | |
479 | } | |
480 | } | |
481 | return 1; | |
482 | } | |
483 | ||
484 | static int hw_rule_rate(struct snd_pcm_hw_params *params, | |
485 | struct snd_pcm_hw_rule *rule) | |
486 | { | |
487 | struct snd_usb_substream *subs = rule->private; | |
488 | struct list_head *p; | |
489 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
490 | unsigned int rmin, rmax; | |
491 | int changed; | |
492 | ||
493 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); | |
494 | changed = 0; | |
495 | rmin = rmax = 0; | |
496 | list_for_each(p, &subs->fmt_list) { | |
497 | struct audioformat *fp; | |
498 | fp = list_entry(p, struct audioformat, list); | |
499 | if (!hw_check_valid_format(subs, params, fp)) | |
500 | continue; | |
501 | if (changed++) { | |
502 | if (rmin > fp->rate_min) | |
503 | rmin = fp->rate_min; | |
504 | if (rmax < fp->rate_max) | |
505 | rmax = fp->rate_max; | |
506 | } else { | |
507 | rmin = fp->rate_min; | |
508 | rmax = fp->rate_max; | |
509 | } | |
510 | } | |
511 | ||
512 | if (!changed) { | |
513 | hwc_debug(" --> get empty\n"); | |
514 | it->empty = 1; | |
515 | return -EINVAL; | |
516 | } | |
517 | ||
518 | changed = 0; | |
519 | if (it->min < rmin) { | |
520 | it->min = rmin; | |
521 | it->openmin = 0; | |
522 | changed = 1; | |
523 | } | |
524 | if (it->max > rmax) { | |
525 | it->max = rmax; | |
526 | it->openmax = 0; | |
527 | changed = 1; | |
528 | } | |
529 | if (snd_interval_checkempty(it)) { | |
530 | it->empty = 1; | |
531 | return -EINVAL; | |
532 | } | |
533 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
534 | return changed; | |
535 | } | |
536 | ||
537 | ||
538 | static int hw_rule_channels(struct snd_pcm_hw_params *params, | |
539 | struct snd_pcm_hw_rule *rule) | |
540 | { | |
541 | struct snd_usb_substream *subs = rule->private; | |
542 | struct list_head *p; | |
543 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
544 | unsigned int rmin, rmax; | |
545 | int changed; | |
546 | ||
547 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); | |
548 | changed = 0; | |
549 | rmin = rmax = 0; | |
550 | list_for_each(p, &subs->fmt_list) { | |
551 | struct audioformat *fp; | |
552 | fp = list_entry(p, struct audioformat, list); | |
553 | if (!hw_check_valid_format(subs, params, fp)) | |
554 | continue; | |
555 | if (changed++) { | |
556 | if (rmin > fp->channels) | |
557 | rmin = fp->channels; | |
558 | if (rmax < fp->channels) | |
559 | rmax = fp->channels; | |
560 | } else { | |
561 | rmin = fp->channels; | |
562 | rmax = fp->channels; | |
563 | } | |
564 | } | |
565 | ||
566 | if (!changed) { | |
567 | hwc_debug(" --> get empty\n"); | |
568 | it->empty = 1; | |
569 | return -EINVAL; | |
570 | } | |
571 | ||
572 | changed = 0; | |
573 | if (it->min < rmin) { | |
574 | it->min = rmin; | |
575 | it->openmin = 0; | |
576 | changed = 1; | |
577 | } | |
578 | if (it->max > rmax) { | |
579 | it->max = rmax; | |
580 | it->openmax = 0; | |
581 | changed = 1; | |
582 | } | |
583 | if (snd_interval_checkempty(it)) { | |
584 | it->empty = 1; | |
585 | return -EINVAL; | |
586 | } | |
587 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
588 | return changed; | |
589 | } | |
590 | ||
591 | static int hw_rule_format(struct snd_pcm_hw_params *params, | |
592 | struct snd_pcm_hw_rule *rule) | |
593 | { | |
594 | struct snd_usb_substream *subs = rule->private; | |
595 | struct list_head *p; | |
596 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
597 | u64 fbits; | |
598 | u32 oldbits[2]; | |
599 | int changed; | |
600 | ||
601 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); | |
602 | fbits = 0; | |
603 | list_for_each(p, &subs->fmt_list) { | |
604 | struct audioformat *fp; | |
605 | fp = list_entry(p, struct audioformat, list); | |
606 | if (!hw_check_valid_format(subs, params, fp)) | |
607 | continue; | |
015eb0b0 | 608 | fbits |= fp->formats; |
e5779998 DM |
609 | } |
610 | ||
611 | oldbits[0] = fmt->bits[0]; | |
612 | oldbits[1] = fmt->bits[1]; | |
613 | fmt->bits[0] &= (u32)fbits; | |
614 | fmt->bits[1] &= (u32)(fbits >> 32); | |
615 | if (!fmt->bits[0] && !fmt->bits[1]) { | |
616 | hwc_debug(" --> get empty\n"); | |
617 | return -EINVAL; | |
618 | } | |
619 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); | |
620 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); | |
621 | return changed; | |
622 | } | |
623 | ||
624 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, | |
625 | struct snd_pcm_hw_rule *rule) | |
626 | { | |
627 | struct snd_usb_substream *subs = rule->private; | |
628 | struct audioformat *fp; | |
629 | struct snd_interval *it; | |
630 | unsigned char min_datainterval; | |
631 | unsigned int pmin; | |
632 | int changed; | |
633 | ||
634 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
635 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); | |
636 | min_datainterval = 0xff; | |
637 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
638 | if (!hw_check_valid_format(subs, params, fp)) | |
639 | continue; | |
640 | min_datainterval = min(min_datainterval, fp->datainterval); | |
641 | } | |
642 | if (min_datainterval == 0xff) { | |
a7ce2e0d | 643 | hwc_debug(" --> get empty\n"); |
e5779998 DM |
644 | it->empty = 1; |
645 | return -EINVAL; | |
646 | } | |
647 | pmin = 125 * (1 << min_datainterval); | |
648 | changed = 0; | |
649 | if (it->min < pmin) { | |
650 | it->min = pmin; | |
651 | it->openmin = 0; | |
652 | changed = 1; | |
653 | } | |
654 | if (snd_interval_checkempty(it)) { | |
655 | it->empty = 1; | |
656 | return -EINVAL; | |
657 | } | |
658 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); | |
659 | return changed; | |
660 | } | |
661 | ||
662 | /* | |
663 | * If the device supports unusual bit rates, does the request meet these? | |
664 | */ | |
665 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, | |
666 | struct snd_usb_substream *subs) | |
667 | { | |
668 | struct audioformat *fp; | |
669 | int count = 0, needs_knot = 0; | |
670 | int err; | |
671 | ||
672 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
673 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) | |
674 | return 0; | |
675 | count += fp->nr_rates; | |
676 | if (fp->rates & SNDRV_PCM_RATE_KNOT) | |
677 | needs_knot = 1; | |
678 | } | |
679 | if (!needs_knot) | |
680 | return 0; | |
681 | ||
e5779998 | 682 | subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL); |
8a8d56b2 JJ |
683 | if (!subs->rate_list.list) |
684 | return -ENOMEM; | |
685 | subs->rate_list.count = count; | |
e5779998 DM |
686 | subs->rate_list.mask = 0; |
687 | count = 0; | |
688 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
689 | int i; | |
690 | for (i = 0; i < fp->nr_rates; i++) | |
691 | subs->rate_list.list[count++] = fp->rate_table[i]; | |
692 | } | |
693 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
694 | &subs->rate_list); | |
695 | if (err < 0) | |
696 | return err; | |
697 | ||
698 | return 0; | |
699 | } | |
700 | ||
701 | ||
702 | /* | |
703 | * set up the runtime hardware information. | |
704 | */ | |
705 | ||
706 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) | |
707 | { | |
708 | struct list_head *p; | |
709 | unsigned int pt, ptmin; | |
710 | int param_period_time_if_needed; | |
711 | int err; | |
712 | ||
713 | runtime->hw.formats = subs->formats; | |
714 | ||
715 | runtime->hw.rate_min = 0x7fffffff; | |
716 | runtime->hw.rate_max = 0; | |
717 | runtime->hw.channels_min = 256; | |
718 | runtime->hw.channels_max = 0; | |
719 | runtime->hw.rates = 0; | |
720 | ptmin = UINT_MAX; | |
721 | /* check min/max rates and channels */ | |
722 | list_for_each(p, &subs->fmt_list) { | |
723 | struct audioformat *fp; | |
724 | fp = list_entry(p, struct audioformat, list); | |
725 | runtime->hw.rates |= fp->rates; | |
726 | if (runtime->hw.rate_min > fp->rate_min) | |
727 | runtime->hw.rate_min = fp->rate_min; | |
728 | if (runtime->hw.rate_max < fp->rate_max) | |
729 | runtime->hw.rate_max = fp->rate_max; | |
730 | if (runtime->hw.channels_min > fp->channels) | |
731 | runtime->hw.channels_min = fp->channels; | |
732 | if (runtime->hw.channels_max < fp->channels) | |
733 | runtime->hw.channels_max = fp->channels; | |
734 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { | |
735 | /* FIXME: there might be more than one audio formats... */ | |
736 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = | |
737 | fp->frame_size; | |
738 | } | |
739 | pt = 125 * (1 << fp->datainterval); | |
740 | ptmin = min(ptmin, pt); | |
741 | } | |
742 | ||
743 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; | |
4f4e8f69 | 744 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
e5779998 DM |
745 | /* full speed devices have fixed data packet interval */ |
746 | ptmin = 1000; | |
747 | if (ptmin == 1000) | |
748 | /* if period time doesn't go below 1 ms, no rules needed */ | |
749 | param_period_time_if_needed = -1; | |
750 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
751 | ptmin, UINT_MAX); | |
752 | ||
753 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
754 | hw_rule_rate, subs, | |
755 | SNDRV_PCM_HW_PARAM_FORMAT, | |
756 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
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_CHANNELS, | |
761 | hw_rule_channels, subs, | |
762 | SNDRV_PCM_HW_PARAM_FORMAT, | |
763 | SNDRV_PCM_HW_PARAM_RATE, | |
764 | param_period_time_if_needed, | |
765 | -1)) < 0) | |
766 | return err; | |
767 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, | |
768 | hw_rule_format, subs, | |
769 | SNDRV_PCM_HW_PARAM_RATE, | |
770 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
771 | param_period_time_if_needed, | |
772 | -1)) < 0) | |
773 | return err; | |
774 | if (param_period_time_if_needed >= 0) { | |
775 | err = snd_pcm_hw_rule_add(runtime, 0, | |
776 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
777 | hw_rule_period_time, subs, | |
778 | SNDRV_PCM_HW_PARAM_FORMAT, | |
779 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
780 | SNDRV_PCM_HW_PARAM_RATE, | |
781 | -1); | |
782 | if (err < 0) | |
783 | return err; | |
784 | } | |
785 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) | |
786 | return err; | |
787 | return 0; | |
788 | } | |
789 | ||
790 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) | |
791 | { | |
792 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
793 | struct snd_pcm_runtime *runtime = substream->runtime; | |
794 | struct snd_usb_substream *subs = &as->substream[direction]; | |
795 | ||
796 | subs->interface = -1; | |
e11b4e0e | 797 | subs->altset_idx = 0; |
e5779998 DM |
798 | runtime->hw = snd_usb_hardware; |
799 | runtime->private_data = subs; | |
800 | subs->pcm_substream = substream; | |
801 | return setup_hw_info(runtime, subs); | |
802 | } | |
803 | ||
804 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) | |
805 | { | |
806 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
807 | struct snd_usb_substream *subs = &as->substream[direction]; | |
808 | ||
809 | if (!as->chip->shutdown && subs->interface >= 0) { | |
810 | usb_set_interface(subs->dev, subs->interface, 0); | |
811 | subs->interface = -1; | |
812 | } | |
813 | subs->pcm_substream = NULL; | |
814 | return 0; | |
815 | } | |
816 | ||
817 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) | |
818 | { | |
819 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
820 | } | |
821 | ||
822 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) | |
823 | { | |
824 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
825 | } | |
826 | ||
827 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) | |
828 | { | |
829 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); | |
830 | } | |
831 | ||
832 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) | |
833 | { | |
834 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); | |
835 | } | |
836 | ||
837 | static struct snd_pcm_ops snd_usb_playback_ops = { | |
838 | .open = snd_usb_playback_open, | |
839 | .close = snd_usb_playback_close, | |
840 | .ioctl = snd_pcm_lib_ioctl, | |
841 | .hw_params = snd_usb_hw_params, | |
842 | .hw_free = snd_usb_hw_free, | |
843 | .prepare = snd_usb_pcm_prepare, | |
844 | .trigger = snd_usb_substream_playback_trigger, | |
845 | .pointer = snd_usb_pcm_pointer, | |
846 | .page = snd_pcm_lib_get_vmalloc_page, | |
847 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
848 | }; | |
849 | ||
850 | static struct snd_pcm_ops snd_usb_capture_ops = { | |
851 | .open = snd_usb_capture_open, | |
852 | .close = snd_usb_capture_close, | |
853 | .ioctl = snd_pcm_lib_ioctl, | |
854 | .hw_params = snd_usb_hw_params, | |
855 | .hw_free = snd_usb_hw_free, | |
856 | .prepare = snd_usb_pcm_prepare, | |
857 | .trigger = snd_usb_substream_capture_trigger, | |
858 | .pointer = snd_usb_pcm_pointer, | |
859 | .page = snd_pcm_lib_get_vmalloc_page, | |
860 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
861 | }; | |
862 | ||
863 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) | |
864 | { | |
865 | snd_pcm_set_ops(pcm, stream, | |
866 | stream == SNDRV_PCM_STREAM_PLAYBACK ? | |
867 | &snd_usb_playback_ops : &snd_usb_capture_ops); | |
868 | } |