Commit | Line | Data |
---|---|---|
e5779998 DM |
1 | /* |
2 | * (Tentative) USB Audio Driver for ALSA | |
3 | * | |
4 | * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> | |
5 | * | |
6 | * Many codes borrowed from audio.c by | |
7 | * Alan Cox (alan@lxorguk.ukuu.org.uk) | |
8 | * Thomas Sailer (sailer@ife.ee.ethz.ch) | |
9 | * | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | * | |
25 | * | |
26 | * NOTES: | |
27 | * | |
e5779998 DM |
28 | * - the linked URBs would be preferred but not used so far because of |
29 | * the instability of unlinking. | |
30 | * - type II is not supported properly. there is no device which supports | |
31 | * this type *correctly*. SB extigy looks as if it supports, but it's | |
32 | * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream). | |
33 | */ | |
34 | ||
35 | ||
36 | #include <linux/bitops.h> | |
37 | #include <linux/init.h> | |
38 | #include <linux/list.h> | |
39 | #include <linux/slab.h> | |
40 | #include <linux/string.h> | |
3ffc1222 | 41 | #include <linux/ctype.h> |
e5779998 DM |
42 | #include <linux/usb.h> |
43 | #include <linux/moduleparam.h> | |
44 | #include <linux/mutex.h> | |
45 | #include <linux/usb/audio.h> | |
7e847894 | 46 | #include <linux/usb/audio-v2.h> |
da155d5b | 47 | #include <linux/module.h> |
e5779998 | 48 | |
9e38658f | 49 | #include <sound/control.h> |
e5779998 DM |
50 | #include <sound/core.h> |
51 | #include <sound/info.h> | |
52 | #include <sound/pcm.h> | |
53 | #include <sound/pcm_params.h> | |
54 | #include <sound/initval.h> | |
55 | ||
56 | #include "usbaudio.h" | |
57 | #include "card.h" | |
58 | #include "midi.h" | |
f0b5e634 | 59 | #include "mixer.h" |
e5779998 DM |
60 | #include "proc.h" |
61 | #include "quirks.h" | |
62 | #include "endpoint.h" | |
63 | #include "helper.h" | |
64 | #include "debug.h" | |
65 | #include "pcm.h" | |
e5779998 | 66 | #include "format.h" |
88a8516a | 67 | #include "power.h" |
e8e8babf | 68 | #include "stream.h" |
e5779998 DM |
69 | |
70 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); | |
71 | MODULE_DESCRIPTION("USB Audio"); | |
72 | MODULE_LICENSE("GPL"); | |
73 | MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}"); | |
74 | ||
75 | ||
76 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ | |
77 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ | |
a67ff6a5 | 78 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ |
e5779998 DM |
79 | /* Vendor/product IDs for this card */ |
80 | static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; | |
81 | static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; | |
82 | static int nrpacks = 8; /* max. number of packets per urb */ | |
e5779998 | 83 | static int device_setup[SNDRV_CARDS]; /* device parameter for this card */ |
a67ff6a5 | 84 | static bool ignore_ctl_error; |
ef02e29b | 85 | static bool autoclock = true; |
e5779998 DM |
86 | |
87 | module_param_array(index, int, NULL, 0444); | |
88 | MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); | |
89 | module_param_array(id, charp, NULL, 0444); | |
90 | MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); | |
91 | module_param_array(enable, bool, NULL, 0444); | |
92 | MODULE_PARM_DESC(enable, "Enable USB audio adapter."); | |
93 | module_param_array(vid, int, NULL, 0444); | |
94 | MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); | |
95 | module_param_array(pid, int, NULL, 0444); | |
96 | MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); | |
97 | module_param(nrpacks, int, 0644); | |
98 | MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB."); | |
e5779998 DM |
99 | module_param_array(device_setup, int, NULL, 0444); |
100 | MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); | |
101 | module_param(ignore_ctl_error, bool, 0444); | |
102 | MODULE_PARM_DESC(ignore_ctl_error, | |
103 | "Ignore errors from USB controller for mixer interfaces."); | |
ef02e29b EZ |
104 | module_param(autoclock, bool, 0444); |
105 | MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes)."); | |
e5779998 DM |
106 | |
107 | /* | |
108 | * we keep the snd_usb_audio_t instances by ourselves for merging | |
109 | * the all interfaces on the same card as one sound device. | |
110 | */ | |
111 | ||
112 | static DEFINE_MUTEX(register_mutex); | |
113 | static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; | |
114 | static struct usb_driver usb_audio_driver; | |
115 | ||
116 | /* | |
117 | * disconnect streams | |
118 | * called from snd_usb_audio_disconnect() | |
119 | */ | |
120 | static void snd_usb_stream_disconnect(struct list_head *head) | |
121 | { | |
122 | int idx; | |
123 | struct snd_usb_stream *as; | |
124 | struct snd_usb_substream *subs; | |
125 | ||
126 | as = list_entry(head, struct snd_usb_stream, list); | |
127 | for (idx = 0; idx < 2; idx++) { | |
128 | subs = &as->substream[idx]; | |
129 | if (!subs->num_formats) | |
76195fb0 | 130 | continue; |
e5779998 | 131 | subs->interface = -1; |
edcd3633 DM |
132 | subs->data_endpoint = NULL; |
133 | subs->sync_endpoint = NULL; | |
e5779998 DM |
134 | } |
135 | } | |
136 | ||
137 | static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) | |
138 | { | |
139 | struct usb_device *dev = chip->dev; | |
140 | struct usb_host_interface *alts; | |
141 | struct usb_interface_descriptor *altsd; | |
142 | struct usb_interface *iface = usb_ifnum_to_if(dev, interface); | |
143 | ||
144 | if (!iface) { | |
145 | snd_printk(KERN_ERR "%d:%u:%d : does not exist\n", | |
146 | dev->devnum, ctrlif, interface); | |
147 | return -EINVAL; | |
148 | } | |
149 | ||
342cda29 CL |
150 | alts = &iface->altsetting[0]; |
151 | altsd = get_iface_desc(alts); | |
152 | ||
153 | /* | |
154 | * Android with both accessory and audio interfaces enabled gets the | |
155 | * interface numbers wrong. | |
156 | */ | |
157 | if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) || | |
158 | chip->usb_id == USB_ID(0x18d1, 0x2d05)) && | |
159 | interface == 0 && | |
160 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && | |
161 | altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) { | |
162 | interface = 2; | |
163 | iface = usb_ifnum_to_if(dev, interface); | |
164 | if (!iface) | |
165 | return -EINVAL; | |
166 | alts = &iface->altsetting[0]; | |
167 | altsd = get_iface_desc(alts); | |
168 | } | |
169 | ||
e5779998 DM |
170 | if (usb_interface_claimed(iface)) { |
171 | snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", | |
172 | dev->devnum, ctrlif, interface); | |
173 | return -EINVAL; | |
174 | } | |
175 | ||
e5779998 DM |
176 | if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || |
177 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && | |
178 | altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) { | |
179 | int err = snd_usbmidi_create(chip->card, iface, | |
180 | &chip->midi_list, NULL); | |
181 | if (err < 0) { | |
182 | snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", | |
183 | dev->devnum, ctrlif, interface); | |
184 | return -EINVAL; | |
185 | } | |
186 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && | |
192 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || | |
193 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) { | |
194 | snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", | |
195 | dev->devnum, ctrlif, interface, altsd->bInterfaceClass); | |
196 | /* skip non-supported classes */ | |
197 | return -EINVAL; | |
198 | } | |
199 | ||
200 | if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { | |
201 | snd_printk(KERN_ERR "low speed audio streaming not supported\n"); | |
202 | return -EINVAL; | |
203 | } | |
204 | ||
e8e8babf | 205 | if (! snd_usb_parse_audio_interface(chip, interface)) { |
e5779998 DM |
206 | usb_set_interface(dev, interface, 0); /* reset the current interface */ |
207 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
208 | return -EINVAL; | |
209 | } | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
214 | /* | |
215 | * parse audio control descriptor and create pcm/midi streams | |
216 | */ | |
217 | static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) | |
218 | { | |
219 | struct usb_device *dev = chip->dev; | |
220 | struct usb_host_interface *host_iface; | |
221 | struct usb_interface_descriptor *altsd; | |
222 | void *control_header; | |
223 | int i, protocol; | |
224 | ||
225 | /* find audiocontrol interface */ | |
226 | host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; | |
227 | control_header = snd_usb_find_csint_desc(host_iface->extra, | |
228 | host_iface->extralen, | |
229 | NULL, UAC_HEADER); | |
230 | altsd = get_iface_desc(host_iface); | |
231 | protocol = altsd->bInterfaceProtocol; | |
232 | ||
233 | if (!control_header) { | |
234 | snd_printk(KERN_ERR "cannot find UAC_HEADER\n"); | |
235 | return -EINVAL; | |
236 | } | |
237 | ||
238 | switch (protocol) { | |
a2acad82 CL |
239 | default: |
240 | snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n", | |
241 | protocol); | |
242 | /* fall through */ | |
243 | ||
e5779998 | 244 | case UAC_VERSION_1: { |
69da9bcb | 245 | struct uac1_ac_header_descriptor *h1 = control_header; |
e5779998 DM |
246 | |
247 | if (!h1->bInCollection) { | |
248 | snd_printk(KERN_INFO "skipping empty audio interface (v1)\n"); | |
249 | return -EINVAL; | |
250 | } | |
251 | ||
252 | if (h1->bLength < sizeof(*h1) + h1->bInCollection) { | |
253 | snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n"); | |
254 | return -EINVAL; | |
255 | } | |
256 | ||
257 | for (i = 0; i < h1->bInCollection; i++) | |
258 | snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); | |
259 | ||
260 | break; | |
261 | } | |
262 | ||
263 | case UAC_VERSION_2: { | |
e5779998 DM |
264 | struct usb_interface_assoc_descriptor *assoc = |
265 | usb_ifnum_to_if(dev, ctrlif)->intf_assoc; | |
266 | ||
281a6ac0 CL |
267 | if (!assoc) { |
268 | /* | |
269 | * Firmware writers cannot count to three. So to find | |
270 | * the IAD on the NuForce UDH-100, also check the next | |
271 | * interface. | |
272 | */ | |
273 | struct usb_interface *iface = | |
274 | usb_ifnum_to_if(dev, ctrlif + 1); | |
275 | if (iface && | |
276 | iface->intf_assoc && | |
277 | iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO && | |
278 | iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2) | |
279 | assoc = iface->intf_assoc; | |
280 | } | |
281 | ||
e5779998 DM |
282 | if (!assoc) { |
283 | snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n"); | |
284 | return -EINVAL; | |
285 | } | |
286 | ||
e5779998 DM |
287 | for (i = 0; i < assoc->bInterfaceCount; i++) { |
288 | int intf = assoc->bFirstInterface + i; | |
289 | ||
290 | if (intf != ctrlif) | |
291 | snd_usb_create_stream(chip, ctrlif, intf); | |
292 | } | |
293 | ||
294 | break; | |
295 | } | |
e5779998 DM |
296 | } |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | /* | |
302 | * free the chip instance | |
303 | * | |
304 | * here we have to do not much, since pcm and controls are already freed | |
305 | * | |
306 | */ | |
307 | ||
308 | static int snd_usb_audio_free(struct snd_usb_audio *chip) | |
309 | { | |
596580d0 | 310 | mutex_destroy(&chip->mutex); |
e5779998 DM |
311 | kfree(chip); |
312 | return 0; | |
313 | } | |
314 | ||
315 | static int snd_usb_audio_dev_free(struct snd_device *device) | |
316 | { | |
317 | struct snd_usb_audio *chip = device->device_data; | |
318 | return snd_usb_audio_free(chip); | |
319 | } | |
320 | ||
3ffc1222 TI |
321 | static void remove_trailing_spaces(char *str) |
322 | { | |
323 | char *p; | |
324 | ||
325 | if (!*str) | |
326 | return; | |
327 | for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--) | |
328 | *p = 0; | |
329 | } | |
e5779998 DM |
330 | |
331 | /* | |
332 | * create a chip instance and set its names. | |
333 | */ | |
334 | static int snd_usb_audio_create(struct usb_device *dev, int idx, | |
335 | const struct snd_usb_audio_quirk *quirk, | |
336 | struct snd_usb_audio **rchip) | |
337 | { | |
338 | struct snd_card *card; | |
339 | struct snd_usb_audio *chip; | |
340 | int err, len; | |
341 | char component[14]; | |
342 | static struct snd_device_ops ops = { | |
343 | .dev_free = snd_usb_audio_dev_free, | |
344 | }; | |
345 | ||
346 | *rchip = NULL; | |
347 | ||
4f4e8f69 PZ |
348 | switch (snd_usb_get_speed(dev)) { |
349 | case USB_SPEED_LOW: | |
350 | case USB_SPEED_FULL: | |
351 | case USB_SPEED_HIGH: | |
352 | case USB_SPEED_SUPER: | |
353 | break; | |
354 | default: | |
e5779998 DM |
355 | snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev)); |
356 | return -ENXIO; | |
357 | } | |
358 | ||
359 | err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card); | |
360 | if (err < 0) { | |
361 | snd_printk(KERN_ERR "cannot create card instance %d\n", idx); | |
362 | return err; | |
363 | } | |
364 | ||
365 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); | |
366 | if (! chip) { | |
367 | snd_card_free(card); | |
368 | return -ENOMEM; | |
369 | } | |
370 | ||
596580d0 | 371 | mutex_init(&chip->mutex); |
34f3c89f | 372 | init_rwsem(&chip->shutdown_rwsem); |
e5779998 DM |
373 | chip->index = idx; |
374 | chip->dev = dev; | |
375 | chip->card = card; | |
376 | chip->setup = device_setup[idx]; | |
377 | chip->nrpacks = nrpacks; | |
ef02e29b | 378 | chip->autoclock = autoclock; |
88a8516a | 379 | chip->probing = 1; |
e5779998 DM |
380 | |
381 | chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), | |
382 | le16_to_cpu(dev->descriptor.idProduct)); | |
383 | INIT_LIST_HEAD(&chip->pcm_list); | |
edcd3633 | 384 | INIT_LIST_HEAD(&chip->ep_list); |
e5779998 DM |
385 | INIT_LIST_HEAD(&chip->midi_list); |
386 | INIT_LIST_HEAD(&chip->mixer_list); | |
387 | ||
388 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { | |
389 | snd_usb_audio_free(chip); | |
390 | snd_card_free(card); | |
391 | return err; | |
392 | } | |
393 | ||
394 | strcpy(card->driver, "USB-Audio"); | |
395 | sprintf(component, "USB%04x:%04x", | |
396 | USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); | |
397 | snd_component_add(card, component); | |
398 | ||
399 | /* retrieve the device string as shortname */ | |
3ffc1222 | 400 | if (quirk && quirk->product_name && *quirk->product_name) { |
e5779998 DM |
401 | strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname)); |
402 | } else { | |
403 | if (!dev->descriptor.iProduct || | |
404 | usb_string(dev, dev->descriptor.iProduct, | |
405 | card->shortname, sizeof(card->shortname)) <= 0) { | |
406 | /* no name available from anywhere, so use ID */ | |
407 | sprintf(card->shortname, "USB Device %#04x:%#04x", | |
408 | USB_ID_VENDOR(chip->usb_id), | |
409 | USB_ID_PRODUCT(chip->usb_id)); | |
410 | } | |
411 | } | |
3ffc1222 | 412 | remove_trailing_spaces(card->shortname); |
e5779998 DM |
413 | |
414 | /* retrieve the vendor and device strings as longname */ | |
3ffc1222 | 415 | if (quirk && quirk->vendor_name && *quirk->vendor_name) { |
e5779998 DM |
416 | len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname)); |
417 | } else { | |
418 | if (dev->descriptor.iManufacturer) | |
419 | len = usb_string(dev, dev->descriptor.iManufacturer, | |
420 | card->longname, sizeof(card->longname)); | |
421 | else | |
422 | len = 0; | |
423 | /* we don't really care if there isn't any vendor string */ | |
424 | } | |
3ffc1222 TI |
425 | if (len > 0) { |
426 | remove_trailing_spaces(card->longname); | |
427 | if (*card->longname) | |
428 | strlcat(card->longname, " ", sizeof(card->longname)); | |
429 | } | |
e5779998 DM |
430 | |
431 | strlcat(card->longname, card->shortname, sizeof(card->longname)); | |
432 | ||
433 | len = strlcat(card->longname, " at ", sizeof(card->longname)); | |
434 | ||
435 | if (len < sizeof(card->longname)) | |
436 | usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); | |
437 | ||
4f4e8f69 PZ |
438 | switch (snd_usb_get_speed(dev)) { |
439 | case USB_SPEED_LOW: | |
440 | strlcat(card->longname, ", low speed", sizeof(card->longname)); | |
441 | break; | |
442 | case USB_SPEED_FULL: | |
443 | strlcat(card->longname, ", full speed", sizeof(card->longname)); | |
444 | break; | |
445 | case USB_SPEED_HIGH: | |
446 | strlcat(card->longname, ", high speed", sizeof(card->longname)); | |
447 | break; | |
448 | case USB_SPEED_SUPER: | |
449 | strlcat(card->longname, ", super speed", sizeof(card->longname)); | |
450 | break; | |
451 | default: | |
452 | break; | |
453 | } | |
e5779998 DM |
454 | |
455 | snd_usb_audio_create_proc(chip); | |
456 | ||
457 | *rchip = chip; | |
458 | return 0; | |
459 | } | |
460 | ||
461 | /* | |
462 | * probe the active usb device | |
463 | * | |
464 | * note that this can be called multiple times per a device, when it | |
465 | * includes multiple audio control interfaces. | |
466 | * | |
467 | * thus we check the usb device pointer and creates the card instance | |
468 | * only at the first time. the successive calls of this function will | |
469 | * append the pcm interface to the corresponding card. | |
470 | */ | |
81b85b6b PR |
471 | static struct snd_usb_audio * |
472 | snd_usb_audio_probe(struct usb_device *dev, | |
473 | struct usb_interface *intf, | |
474 | const struct usb_device_id *usb_id) | |
e5779998 DM |
475 | { |
476 | const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info; | |
477 | int i, err; | |
478 | struct snd_usb_audio *chip; | |
479 | struct usb_host_interface *alts; | |
480 | int ifnum; | |
481 | u32 id; | |
482 | ||
483 | alts = &intf->altsetting[0]; | |
484 | ifnum = get_iface_desc(alts)->bInterfaceNumber; | |
485 | id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), | |
486 | le16_to_cpu(dev->descriptor.idProduct)); | |
487 | if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) | |
488 | goto __err_val; | |
489 | ||
490 | if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0) | |
491 | goto __err_val; | |
492 | ||
493 | /* | |
494 | * found a config. now register to ALSA | |
495 | */ | |
496 | ||
497 | /* check whether it's already registered */ | |
498 | chip = NULL; | |
499 | mutex_lock(®ister_mutex); | |
500 | for (i = 0; i < SNDRV_CARDS; i++) { | |
501 | if (usb_chip[i] && usb_chip[i]->dev == dev) { | |
502 | if (usb_chip[i]->shutdown) { | |
503 | snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n"); | |
504 | goto __error; | |
505 | } | |
506 | chip = usb_chip[i]; | |
88a8516a | 507 | chip->probing = 1; |
e5779998 DM |
508 | break; |
509 | } | |
510 | } | |
511 | if (! chip) { | |
512 | /* it's a fresh one. | |
513 | * now look for an empty slot and create a new card instance | |
514 | */ | |
515 | for (i = 0; i < SNDRV_CARDS; i++) | |
516 | if (enable[i] && ! usb_chip[i] && | |
517 | (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && | |
518 | (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { | |
519 | if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) { | |
520 | goto __error; | |
521 | } | |
522 | snd_card_set_dev(chip->card, &intf->dev); | |
88a8516a | 523 | chip->pm_intf = intf; |
e5779998 DM |
524 | break; |
525 | } | |
526 | if (!chip) { | |
527 | printk(KERN_ERR "no available usb audio device\n"); | |
528 | goto __error; | |
529 | } | |
530 | } | |
531 | ||
7b6717e1 DM |
532 | /* |
533 | * For devices with more than one control interface, we assume the | |
534 | * first contains the audio controls. We might need a more specific | |
535 | * check here in the future. | |
536 | */ | |
537 | if (!chip->ctrl_intf) | |
538 | chip->ctrl_intf = alts; | |
79f920fb | 539 | |
5875c2cb DM |
540 | chip->txfr_quirk = 0; |
541 | err = 1; /* continue */ | |
542 | if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { | |
543 | /* need some special handlings */ | |
544 | if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0) | |
545 | goto __error; | |
546 | } | |
547 | ||
e5779998 DM |
548 | if (err > 0) { |
549 | /* create normal USB audio interfaces */ | |
550 | if (snd_usb_create_streams(chip, ifnum) < 0 || | |
551 | snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) { | |
552 | goto __error; | |
553 | } | |
554 | } | |
555 | ||
556 | /* we are allowed to call snd_card_register() many times */ | |
557 | if (snd_card_register(chip->card) < 0) { | |
558 | goto __error; | |
559 | } | |
560 | ||
561 | usb_chip[chip->index] = chip; | |
562 | chip->num_interfaces++; | |
88a8516a | 563 | chip->probing = 0; |
e5779998 DM |
564 | mutex_unlock(®ister_mutex); |
565 | return chip; | |
566 | ||
567 | __error: | |
61a6a108 TP |
568 | if (chip) { |
569 | if (!chip->num_interfaces) | |
570 | snd_card_free(chip->card); | |
571 | chip->probing = 0; | |
572 | } | |
e5779998 DM |
573 | mutex_unlock(®ister_mutex); |
574 | __err_val: | |
575 | return NULL; | |
576 | } | |
577 | ||
578 | /* | |
579 | * we need to take care of counter, since disconnection can be called also | |
580 | * many times as well as usb_audio_probe(). | |
581 | */ | |
81b85b6b PR |
582 | static void snd_usb_audio_disconnect(struct usb_device *dev, |
583 | struct snd_usb_audio *chip) | |
e5779998 | 584 | { |
e5779998 | 585 | struct snd_card *card; |
03d2f44e | 586 | struct list_head *p, *n; |
e5779998 | 587 | |
81b85b6b | 588 | if (chip == (void *)-1L) |
e5779998 DM |
589 | return; |
590 | ||
e5779998 | 591 | card = chip->card; |
34f3c89f | 592 | down_write(&chip->shutdown_rwsem); |
e5779998 | 593 | chip->shutdown = 1; |
10e44239 TI |
594 | up_write(&chip->shutdown_rwsem); |
595 | ||
596 | mutex_lock(®ister_mutex); | |
e5779998 DM |
597 | chip->num_interfaces--; |
598 | if (chip->num_interfaces <= 0) { | |
599 | snd_card_disconnect(card); | |
600 | /* release the pcm resources */ | |
601 | list_for_each(p, &chip->pcm_list) { | |
602 | snd_usb_stream_disconnect(p); | |
603 | } | |
edcd3633 | 604 | /* release the endpoint resources */ |
03d2f44e | 605 | list_for_each_safe(p, n, &chip->ep_list) { |
edcd3633 DM |
606 | snd_usb_endpoint_free(p); |
607 | } | |
e5779998 DM |
608 | /* release the midi resources */ |
609 | list_for_each(p, &chip->midi_list) { | |
610 | snd_usbmidi_disconnect(p); | |
611 | } | |
612 | /* release mixer resources */ | |
613 | list_for_each(p, &chip->mixer_list) { | |
614 | snd_usb_mixer_disconnect(p); | |
615 | } | |
616 | usb_chip[chip->index] = NULL; | |
617 | mutex_unlock(®ister_mutex); | |
618 | snd_card_free_when_closed(card); | |
619 | } else { | |
620 | mutex_unlock(®ister_mutex); | |
621 | } | |
622 | } | |
623 | ||
624 | /* | |
625 | * new 2.5 USB kernel API | |
626 | */ | |
627 | static int usb_audio_probe(struct usb_interface *intf, | |
628 | const struct usb_device_id *id) | |
629 | { | |
81b85b6b | 630 | struct snd_usb_audio *chip; |
e5779998 DM |
631 | chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id); |
632 | if (chip) { | |
633 | usb_set_intfdata(intf, chip); | |
634 | return 0; | |
635 | } else | |
636 | return -EIO; | |
637 | } | |
638 | ||
639 | static void usb_audio_disconnect(struct usb_interface *intf) | |
640 | { | |
641 | snd_usb_audio_disconnect(interface_to_usbdev(intf), | |
642 | usb_get_intfdata(intf)); | |
643 | } | |
644 | ||
645 | #ifdef CONFIG_PM | |
88a8516a ON |
646 | |
647 | int snd_usb_autoresume(struct snd_usb_audio *chip) | |
648 | { | |
649 | int err = -ENODEV; | |
650 | ||
34f3c89f | 651 | down_read(&chip->shutdown_rwsem); |
60af3d03 TI |
652 | if (chip->probing) |
653 | err = 0; | |
654 | else if (!chip->shutdown) | |
88a8516a | 655 | err = usb_autopm_get_interface(chip->pm_intf); |
34f3c89f | 656 | up_read(&chip->shutdown_rwsem); |
88a8516a ON |
657 | |
658 | return err; | |
659 | } | |
660 | ||
661 | void snd_usb_autosuspend(struct snd_usb_audio *chip) | |
662 | { | |
34f3c89f | 663 | down_read(&chip->shutdown_rwsem); |
88a8516a ON |
664 | if (!chip->shutdown && !chip->probing) |
665 | usb_autopm_put_interface(chip->pm_intf); | |
34f3c89f | 666 | up_read(&chip->shutdown_rwsem); |
88a8516a ON |
667 | } |
668 | ||
e5779998 DM |
669 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) |
670 | { | |
671 | struct snd_usb_audio *chip = usb_get_intfdata(intf); | |
e5779998 | 672 | struct snd_usb_stream *as; |
edf7de31 | 673 | struct usb_mixer_interface *mixer; |
e5779998 DM |
674 | |
675 | if (chip == (void *)-1L) | |
676 | return 0; | |
677 | ||
5b1b0b81 | 678 | if (!PMSG_IS_AUTO(message)) { |
88a8516a ON |
679 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); |
680 | if (!chip->num_suspended_intf++) { | |
88766f04 | 681 | list_for_each_entry(as, &chip->pcm_list, list) { |
88a8516a | 682 | snd_pcm_suspend_all(as->pcm); |
384dc085 TI |
683 | as->substream[0].need_setup_ep = |
684 | as->substream[1].need_setup_ep = true; | |
88a8516a | 685 | } |
aa53f986 | 686 | } |
88a8516a ON |
687 | } else { |
688 | /* | |
689 | * otherwise we keep the rest of the system in the dark | |
690 | * to keep this transparent | |
691 | */ | |
692 | if (!chip->num_suspended_intf++) | |
693 | chip->autosuspended = 1; | |
e5779998 DM |
694 | } |
695 | ||
88a8516a ON |
696 | list_for_each_entry(mixer, &chip->mixer_list, list) |
697 | snd_usb_mixer_inactivate(mixer); | |
698 | ||
e5779998 DM |
699 | return 0; |
700 | } | |
701 | ||
702 | static int usb_audio_resume(struct usb_interface *intf) | |
703 | { | |
704 | struct snd_usb_audio *chip = usb_get_intfdata(intf); | |
edf7de31 | 705 | struct usb_mixer_interface *mixer; |
88a8516a | 706 | int err = 0; |
e5779998 DM |
707 | |
708 | if (chip == (void *)-1L) | |
709 | return 0; | |
710 | if (--chip->num_suspended_intf) | |
711 | return 0; | |
712 | /* | |
713 | * ALSA leaves material resumption to user space | |
edf7de31 | 714 | * we just notify and restart the mixers |
e5779998 | 715 | */ |
88a8516a ON |
716 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
717 | err = snd_usb_mixer_activate(mixer); | |
718 | if (err < 0) | |
719 | goto err_out; | |
720 | } | |
e5779998 | 721 | |
88a8516a ON |
722 | if (!chip->autosuspended) |
723 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); | |
724 | chip->autosuspended = 0; | |
e5779998 | 725 | |
88a8516a ON |
726 | err_out: |
727 | return err; | |
e5779998 | 728 | } |
6407d474 RD |
729 | #else |
730 | #define usb_audio_suspend NULL | |
731 | #define usb_audio_resume NULL | |
e5779998 DM |
732 | #endif /* CONFIG_PM */ |
733 | ||
734 | static struct usb_device_id usb_audio_ids [] = { | |
735 | #include "quirks-table.h" | |
736 | { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), | |
737 | .bInterfaceClass = USB_CLASS_AUDIO, | |
738 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL }, | |
739 | { } /* Terminating entry */ | |
740 | }; | |
f9d35435 | 741 | MODULE_DEVICE_TABLE(usb, usb_audio_ids); |
e5779998 DM |
742 | |
743 | /* | |
744 | * entry point for linux usb interface | |
745 | */ | |
746 | ||
747 | static struct usb_driver usb_audio_driver = { | |
748 | .name = "snd-usb-audio", | |
749 | .probe = usb_audio_probe, | |
750 | .disconnect = usb_audio_disconnect, | |
751 | .suspend = usb_audio_suspend, | |
752 | .resume = usb_audio_resume, | |
753 | .id_table = usb_audio_ids, | |
88a8516a | 754 | .supports_autosuspend = 1, |
e5779998 DM |
755 | }; |
756 | ||
757 | static int __init snd_usb_audio_init(void) | |
758 | { | |
759 | if (nrpacks < 1 || nrpacks > MAX_PACKS) { | |
760 | printk(KERN_WARNING "invalid nrpacks value.\n"); | |
761 | return -EINVAL; | |
762 | } | |
763 | return usb_register(&usb_audio_driver); | |
764 | } | |
765 | ||
766 | static void __exit snd_usb_audio_cleanup(void) | |
767 | { | |
768 | usb_deregister(&usb_audio_driver); | |
769 | } | |
770 | ||
771 | module_init(snd_usb_audio_init); | |
772 | module_exit(snd_usb_audio_cleanup); |