~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/usb/card.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  *
 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>
 41 #include <linux/ctype.h>
 42 #include <linux/usb.h>
 43 #include <linux/moduleparam.h>
 44 #include <linux/mutex.h>
 45 #include <linux/usb/audio.h>
 46 #include <linux/usb/audio-v2.h>
 47 #include <linux/module.h>
 48 
 49 #include <sound/control.h>
 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"
 59 #include "mixer.h"
 60 #include "proc.h"
 61 #include "quirks.h"
 62 #include "endpoint.h"
 63 #include "helper.h"
 64 #include "debug.h"
 65 #include "pcm.h"
 66 #include "format.h"
 67 #include "power.h"
 68 #include "stream.h"
 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 */
 78 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
 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 */
 83 static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
 84 static bool ignore_ctl_error;
 85 static bool autoclock = true;
 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.");
 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.");
104 module_param(autoclock, bool, 0444);
105 MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
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)
130                         continue;
131                 subs->interface = -1;
132                 subs->data_endpoint = NULL;
133                 subs->sync_endpoint = NULL;
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 
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 
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 
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 
205         if (! snd_usb_parse_audio_interface(chip, interface)) {
206                 usb_set_interface(dev, interface, 0); /* reset the current interface */
207                 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
208         }
209 
210         return 0;
211 }
212 
213 /*
214  * parse audio control descriptor and create pcm/midi streams
215  */
216 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
217 {
218         struct usb_device *dev = chip->dev;
219         struct usb_host_interface *host_iface;
220         struct usb_interface_descriptor *altsd;
221         void *control_header;
222         int i, protocol;
223 
224         /* find audiocontrol interface */
225         host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
226         control_header = snd_usb_find_csint_desc(host_iface->extra,
227                                                  host_iface->extralen,
228                                                  NULL, UAC_HEADER);
229         altsd = get_iface_desc(host_iface);
230         protocol = altsd->bInterfaceProtocol;
231 
232         if (!control_header) {
233                 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
234                 return -EINVAL;
235         }
236 
237         switch (protocol) {
238         default:
239                 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
240                             protocol);
241                 /* fall through */
242 
243         case UAC_VERSION_1: {
244                 struct uac1_ac_header_descriptor *h1 = control_header;
245 
246                 if (!h1->bInCollection) {
247                         snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
248                         return -EINVAL;
249                 }
250 
251                 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
252                         snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
253                         return -EINVAL;
254                 }
255 
256                 for (i = 0; i < h1->bInCollection; i++)
257                         snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
258 
259                 break;
260         }
261 
262         case UAC_VERSION_2: {
263                 struct usb_interface_assoc_descriptor *assoc =
264                         usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
265 
266                 if (!assoc) {
267                         /*
268                          * Firmware writers cannot count to three.  So to find
269                          * the IAD on the NuForce UDH-100, also check the next
270                          * interface.
271                          */
272                         struct usb_interface *iface =
273                                 usb_ifnum_to_if(dev, ctrlif + 1);
274                         if (iface &&
275                             iface->intf_assoc &&
276                             iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
277                             iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
278                                 assoc = iface->intf_assoc;
279                 }
280 
281                 if (!assoc) {
282                         snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
283                         return -EINVAL;
284                 }
285 
286                 for (i = 0; i < assoc->bInterfaceCount; i++) {
287                         int intf = assoc->bFirstInterface + i;
288 
289                         if (intf != ctrlif)
290                                 snd_usb_create_stream(chip, ctrlif, intf);
291                 }
292 
293                 break;
294         }
295         }
296 
297         return 0;
298 }
299 
300 /*
301  * free the chip instance
302  *
303  * here we have to do not much, since pcm and controls are already freed
304  *
305  */
306 
307 static int snd_usb_audio_free(struct snd_usb_audio *chip)
308 {
309         struct list_head *p, *n;
310 
311         list_for_each_safe(p, n, &chip->ep_list)
312                 snd_usb_endpoint_free(p);
313 
314         mutex_destroy(&chip->mutex);
315         kfree(chip);
316         return 0;
317 }
318 
319 static int snd_usb_audio_dev_free(struct snd_device *device)
320 {
321         struct snd_usb_audio *chip = device->device_data;
322         return snd_usb_audio_free(chip);
323 }
324 
325 static void remove_trailing_spaces(char *str)
326 {
327         char *p;
328 
329         if (!*str)
330                 return;
331         for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
332                 *p = 0;
333 }
334 
335 /*
336  * create a chip instance and set its names.
337  */
338 static int snd_usb_audio_create(struct usb_device *dev, int idx,
339                                 const struct snd_usb_audio_quirk *quirk,
340                                 struct snd_usb_audio **rchip)
341 {
342         struct snd_card *card;
343         struct snd_usb_audio *chip;
344         int err, len;
345         char component[14];
346         static struct snd_device_ops ops = {
347                 .dev_free =     snd_usb_audio_dev_free,
348         };
349 
350         *rchip = NULL;
351 
352         switch (snd_usb_get_speed(dev)) {
353         case USB_SPEED_LOW:
354         case USB_SPEED_FULL:
355         case USB_SPEED_HIGH:
356         case USB_SPEED_SUPER:
357                 break;
358         default:
359                 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
360                 return -ENXIO;
361         }
362 
363         err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
364         if (err < 0) {
365                 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
366                 return err;
367         }
368 
369         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
370         if (! chip) {
371                 snd_card_free(card);
372                 return -ENOMEM;
373         }
374 
375         mutex_init(&chip->mutex);
376         init_rwsem(&chip->shutdown_rwsem);
377         chip->index = idx;
378         chip->dev = dev;
379         chip->card = card;
380         chip->setup = device_setup[idx];
381         chip->nrpacks = nrpacks;
382         chip->autoclock = autoclock;
383         chip->probing = 1;
384 
385         chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
386                               le16_to_cpu(dev->descriptor.idProduct));
387         INIT_LIST_HEAD(&chip->pcm_list);
388         INIT_LIST_HEAD(&chip->ep_list);
389         INIT_LIST_HEAD(&chip->midi_list);
390         INIT_LIST_HEAD(&chip->mixer_list);
391 
392         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
393                 snd_usb_audio_free(chip);
394                 snd_card_free(card);
395                 return err;
396         }
397 
398         strcpy(card->driver, "USB-Audio");
399         sprintf(component, "USB%04x:%04x",
400                 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
401         snd_component_add(card, component);
402 
403         /* retrieve the device string as shortname */
404         if (quirk && quirk->product_name && *quirk->product_name) {
405                 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
406         } else {
407                 if (!dev->descriptor.iProduct ||
408                     usb_string(dev, dev->descriptor.iProduct,
409                     card->shortname, sizeof(card->shortname)) <= 0) {
410                         /* no name available from anywhere, so use ID */
411                         sprintf(card->shortname, "USB Device %#04x:%#04x",
412                                 USB_ID_VENDOR(chip->usb_id),
413                                 USB_ID_PRODUCT(chip->usb_id));
414                 }
415         }
416         remove_trailing_spaces(card->shortname);
417 
418         /* retrieve the vendor and device strings as longname */
419         if (quirk && quirk->vendor_name && *quirk->vendor_name) {
420                 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
421         } else {
422                 if (dev->descriptor.iManufacturer)
423                         len = usb_string(dev, dev->descriptor.iManufacturer,
424                                          card->longname, sizeof(card->longname));
425                 else
426                         len = 0;
427                 /* we don't really care if there isn't any vendor string */
428         }
429         if (len > 0) {
430                 remove_trailing_spaces(card->longname);
431                 if (*card->longname)
432                         strlcat(card->longname, " ", sizeof(card->longname));
433         }
434 
435         strlcat(card->longname, card->shortname, sizeof(card->longname));
436 
437         len = strlcat(card->longname, " at ", sizeof(card->longname));
438 
439         if (len < sizeof(card->longname))
440                 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
441 
442         switch (snd_usb_get_speed(dev)) {
443         case USB_SPEED_LOW:
444                 strlcat(card->longname, ", low speed", sizeof(card->longname));
445                 break;
446         case USB_SPEED_FULL:
447                 strlcat(card->longname, ", full speed", sizeof(card->longname));
448                 break;
449         case USB_SPEED_HIGH:
450                 strlcat(card->longname, ", high speed", sizeof(card->longname));
451                 break;
452         case USB_SPEED_SUPER:
453                 strlcat(card->longname, ", super speed", sizeof(card->longname));
454                 break;
455         default:
456                 break;
457         }
458 
459         snd_usb_audio_create_proc(chip);
460 
461         *rchip = chip;
462         return 0;
463 }
464 
465 /*
466  * probe the active usb device
467  *
468  * note that this can be called multiple times per a device, when it
469  * includes multiple audio control interfaces.
470  *
471  * thus we check the usb device pointer and creates the card instance
472  * only at the first time.  the successive calls of this function will
473  * append the pcm interface to the corresponding card.
474  */
475 static struct snd_usb_audio *
476 snd_usb_audio_probe(struct usb_device *dev,
477                     struct usb_interface *intf,
478                     const struct usb_device_id *usb_id)
479 {
480         const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
481         int i, err;
482         struct snd_usb_audio *chip;
483         struct usb_host_interface *alts;
484         int ifnum;
485         u32 id;
486 
487         alts = &intf->altsetting[0];
488         ifnum = get_iface_desc(alts)->bInterfaceNumber;
489         id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
490                     le16_to_cpu(dev->descriptor.idProduct));
491         if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
492                 goto __err_val;
493 
494         if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
495                 goto __err_val;
496 
497         /*
498          * found a config.  now register to ALSA
499          */
500 
501         /* check whether it's already registered */
502         chip = NULL;
503         mutex_lock(&register_mutex);
504         for (i = 0; i < SNDRV_CARDS; i++) {
505                 if (usb_chip[i] && usb_chip[i]->dev == dev) {
506                         if (usb_chip[i]->shutdown) {
507                                 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
508                                 goto __error;
509                         }
510                         chip = usb_chip[i];
511                         chip->probing = 1;
512                         break;
513                 }
514         }
515         if (! chip) {
516                 /* it's a fresh one.
517                  * now look for an empty slot and create a new card instance
518                  */
519                 for (i = 0; i < SNDRV_CARDS; i++)
520                         if (enable[i] && ! usb_chip[i] &&
521                             (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
522                             (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
523                                 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
524                                         goto __error;
525                                 }
526                                 snd_card_set_dev(chip->card, &intf->dev);
527                                 chip->pm_intf = intf;
528                                 break;
529                         }
530                 if (!chip) {
531                         printk(KERN_ERR "no available usb audio device\n");
532                         goto __error;
533                 }
534         }
535 
536         /*
537          * For devices with more than one control interface, we assume the
538          * first contains the audio controls. We might need a more specific
539          * check here in the future.
540          */
541         if (!chip->ctrl_intf)
542                 chip->ctrl_intf = alts;
543 
544         chip->txfr_quirk = 0;
545         err = 1; /* continue */
546         if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
547                 /* need some special handlings */
548                 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
549                         goto __error;
550         }
551 
552         if (err > 0) {
553                 /* create normal USB audio interfaces */
554                 if (snd_usb_create_streams(chip, ifnum) < 0 ||
555                     snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
556                         goto __error;
557                 }
558         }
559 
560         /* we are allowed to call snd_card_register() many times */
561         if (snd_card_register(chip->card) < 0) {
562                 goto __error;
563         }
564 
565         usb_chip[chip->index] = chip;
566         chip->num_interfaces++;
567         chip->probing = 0;
568         mutex_unlock(&register_mutex);
569         return chip;
570 
571  __error:
572         if (chip) {
573                 if (!chip->num_interfaces)
574                         snd_card_free(chip->card);
575                 chip->probing = 0;
576         }
577         mutex_unlock(&register_mutex);
578  __err_val:
579         return NULL;
580 }
581 
582 /*
583  * we need to take care of counter, since disconnection can be called also
584  * many times as well as usb_audio_probe().
585  */
586 static void snd_usb_audio_disconnect(struct usb_device *dev,
587                                      struct snd_usb_audio *chip)
588 {
589         struct snd_card *card;
590         struct list_head *p;
591         bool was_shutdown;
592 
593         if (chip == (void *)-1L)
594                 return;
595 
596         card = chip->card;
597         down_write(&chip->shutdown_rwsem);
598         was_shutdown = chip->shutdown;
599         chip->shutdown = 1;
600         up_write(&chip->shutdown_rwsem);
601 
602         mutex_lock(&register_mutex);
603         if (!was_shutdown) {
604                 struct snd_usb_endpoint *ep;
605 
606                 snd_card_disconnect(card);
607                 /* release the pcm resources */
608                 list_for_each(p, &chip->pcm_list) {
609                         snd_usb_stream_disconnect(p);
610                 }
611                 /* release the endpoint resources */
612                 list_for_each_entry(ep, &chip->ep_list, list) {
613                         snd_usb_endpoint_release(ep);
614                 }
615                 /* release the midi resources */
616                 list_for_each(p, &chip->midi_list) {
617                         snd_usbmidi_disconnect(p);
618                 }
619                 /* release mixer resources */
620                 list_for_each(p, &chip->mixer_list) {
621                         snd_usb_mixer_disconnect(p);
622                 }
623         }
624 
625         chip->num_interfaces--;
626         if (chip->num_interfaces <= 0) {
627                 usb_chip[chip->index] = NULL;
628                 mutex_unlock(&register_mutex);
629                 snd_card_free_when_closed(card);
630         } else {
631                 mutex_unlock(&register_mutex);
632         }
633 }
634 
635 /*
636  * new 2.5 USB kernel API
637  */
638 static int usb_audio_probe(struct usb_interface *intf,
639                            const struct usb_device_id *id)
640 {
641         struct snd_usb_audio *chip;
642         chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
643         if (chip) {
644                 usb_set_intfdata(intf, chip);
645                 return 0;
646         } else
647                 return -EIO;
648 }
649 
650 static void usb_audio_disconnect(struct usb_interface *intf)
651 {
652         snd_usb_audio_disconnect(interface_to_usbdev(intf),
653                                  usb_get_intfdata(intf));
654 }
655 
656 #ifdef CONFIG_PM
657 
658 int snd_usb_autoresume(struct snd_usb_audio *chip)
659 {
660         int err = -ENODEV;
661 
662         down_read(&chip->shutdown_rwsem);
663         if (chip->probing || chip->in_pm)
664                 err = 0;
665         else if (!chip->shutdown)
666                 err = usb_autopm_get_interface(chip->pm_intf);
667         up_read(&chip->shutdown_rwsem);
668 
669         return err;
670 }
671 
672 void snd_usb_autosuspend(struct snd_usb_audio *chip)
673 {
674         down_read(&chip->shutdown_rwsem);
675         if (!chip->shutdown && !chip->probing && !chip->in_pm)
676                 usb_autopm_put_interface(chip->pm_intf);
677         up_read(&chip->shutdown_rwsem);
678 }
679 
680 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
681 {
682         struct snd_usb_audio *chip = usb_get_intfdata(intf);
683         struct snd_usb_stream *as;
684         struct usb_mixer_interface *mixer;
685 
686         if (chip == (void *)-1L)
687                 return 0;
688 
689         if (!PMSG_IS_AUTO(message)) {
690                 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
691                 if (!chip->num_suspended_intf++) {
692                         list_for_each_entry(as, &chip->pcm_list, list) {
693                                 snd_pcm_suspend_all(as->pcm);
694                                 as->substream[0].need_setup_ep =
695                                         as->substream[1].need_setup_ep = true;
696                         }
697                 }
698         } else {
699                 /*
700                  * otherwise we keep the rest of the system in the dark
701                  * to keep this transparent
702                  */
703                 if (!chip->num_suspended_intf++)
704                         chip->autosuspended = 1;
705         }
706 
707         if (chip->num_suspended_intf == 1)
708                 list_for_each_entry(mixer, &chip->mixer_list, list)
709                         snd_usb_mixer_suspend(mixer);
710 
711         return 0;
712 }
713 
714 static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
715 {
716         struct snd_usb_audio *chip = usb_get_intfdata(intf);
717         struct usb_mixer_interface *mixer;
718         int err = 0;
719 
720         if (chip == (void *)-1L)
721                 return 0;
722         if (--chip->num_suspended_intf)
723                 return 0;
724 
725         chip->in_pm = 1;
726         /*
727          * ALSA leaves material resumption to user space
728          * we just notify and restart the mixers
729          */
730         list_for_each_entry(mixer, &chip->mixer_list, list) {
731                 err = snd_usb_mixer_resume(mixer, reset_resume);
732                 if (err < 0)
733                         goto err_out;
734         }
735 
736         if (!chip->autosuspended)
737                 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
738         chip->autosuspended = 0;
739 
740 err_out:
741         chip->in_pm = 0;
742         return err;
743 }
744 
745 static int usb_audio_resume(struct usb_interface *intf)
746 {
747         return __usb_audio_resume(intf, false);
748 }
749 
750 static int usb_audio_reset_resume(struct usb_interface *intf)
751 {
752         return __usb_audio_resume(intf, true);
753 }
754 #else
755 #define usb_audio_suspend       NULL
756 #define usb_audio_resume        NULL
757 #define usb_audio_reset_resume  NULL
758 #endif          /* CONFIG_PM */
759 
760 static struct usb_device_id usb_audio_ids [] = {
761 #include "quirks-table.h"
762     { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
763       .bInterfaceClass = USB_CLASS_AUDIO,
764       .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
765     { }                                         /* Terminating entry */
766 };
767 MODULE_DEVICE_TABLE(usb, usb_audio_ids);
768 
769 /*
770  * entry point for linux usb interface
771  */
772 
773 static struct usb_driver usb_audio_driver = {
774         .name =         "snd-usb-audio",
775         .probe =        usb_audio_probe,
776         .disconnect =   usb_audio_disconnect,
777         .suspend =      usb_audio_suspend,
778         .resume =       usb_audio_resume,
779         .reset_resume = usb_audio_reset_resume,
780         .id_table =     usb_audio_ids,
781         .supports_autosuspend = 1,
782 };
783 
784 static int __init snd_usb_audio_init(void)
785 {
786         if (nrpacks < 1 || nrpacks > MAX_PACKS) {
787                 printk(KERN_WARNING "invalid nrpacks value.\n");
788                 return -EINVAL;
789         }
790         return usb_register(&usb_audio_driver);
791 }
792 
793 static void __exit snd_usb_audio_cleanup(void)
794 {
795         usb_deregister(&usb_audio_driver);
796 }
797 
798 module_init(snd_usb_audio_init);
799 module_exit(snd_usb_audio_cleanup);
800 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp