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