RK3368 DDR: fix HDMI display abnormal when ddr change freq
[firefly-linux-kernel-4.4.55.git] / sound / usb / card.c
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 #ifdef CONFIG_SWITCH
49 #include <linux/switch.h>
50 #endif
51
52 #include <sound/control.h>
53 #include <sound/core.h>
54 #include <sound/info.h>
55 #include <sound/pcm.h>
56 #include <sound/pcm_params.h>
57 #include <sound/initval.h>
58
59 #include "usbaudio.h"
60 #include "card.h"
61 #include "midi.h"
62 #include "mixer.h"
63 #include "proc.h"
64 #include "quirks.h"
65 #include "endpoint.h"
66 #include "helper.h"
67 #include "debug.h"
68 #include "pcm.h"
69 #include "format.h"
70 #include "power.h"
71 #include "stream.h"
72
73 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
74 MODULE_DESCRIPTION("USB Audio");
75 MODULE_LICENSE("GPL");
76 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
77
78
79 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
80 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
81 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
82 /* Vendor/product IDs for this card */
83 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
84 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
85 static int nrpacks = 8;         /* max. number of packets per urb */
86 static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
87 static bool ignore_ctl_error;
88 static bool autoclock = true;
89
90 module_param_array(index, int, NULL, 0444);
91 MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
92 module_param_array(id, charp, NULL, 0444);
93 MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
94 module_param_array(enable, bool, NULL, 0444);
95 MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
96 module_param_array(vid, int, NULL, 0444);
97 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
98 module_param_array(pid, int, NULL, 0444);
99 MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
100 module_param(nrpacks, int, 0644);
101 MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
102 module_param_array(device_setup, int, NULL, 0444);
103 MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
104 module_param(ignore_ctl_error, bool, 0444);
105 MODULE_PARM_DESC(ignore_ctl_error,
106                  "Ignore errors from USB controller for mixer interfaces.");
107 module_param(autoclock, bool, 0444);
108 MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
109
110 /*
111  * we keep the snd_usb_audio_t instances by ourselves for merging
112  * the all interfaces on the same card as one sound device.
113  */
114
115 static DEFINE_MUTEX(register_mutex);
116 static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
117 static struct usb_driver usb_audio_driver;
118 #ifdef CONFIG_SND_RK_SOC
119 #define USB_AUDIO_CARD_NUM      3
120 struct switch_dev *usb_audio_sdev;
121 #endif
122
123 #ifdef CONFIG_SWITCH
124 //usb audio card will begin from RBASE_USB_AUDIO_IDX.
125 #define RBASE_USB_AUDIO_IDX     (3)
126 #define NO_USBAUDIO_PLAYBACK    (-1)
127 #define NO_USBAUDIO_CAPTURE     (-1)
128
129 struct usb_audio_switch {
130         int playback_switch_cur_state;
131         int capture_switch_cur_state;
132         struct switch_dev sUsbaudio_Playback;
133         struct switch_dev sUsbaudio_Capture;
134 };
135
136 // state: card*10 + device
137 static struct usb_audio_switch sUsbaudio_Switch = {
138         .playback_switch_cur_state = NO_USBAUDIO_PLAYBACK,
139         .capture_switch_cur_state = NO_USBAUDIO_CAPTURE,
140         .sUsbaudio_Playback = {
141                 .name = "usb_audio_playback",
142                 .state = NO_USBAUDIO_PLAYBACK, //this means no usb audio playback available
143         },
144
145         .sUsbaudio_Capture = {
146                 .name = "usb_audio_capture",
147                 .state = NO_USBAUDIO_CAPTURE, //this means no usb audio capture available
148         },
149 };
150 #endif
151
152 /*
153  * disconnect streams
154  * called from snd_usb_audio_disconnect()
155  */
156 static void snd_usb_stream_disconnect(struct list_head *head)
157 {
158         int idx;
159         struct snd_usb_stream *as;
160         struct snd_usb_substream *subs;
161
162         as = list_entry(head, struct snd_usb_stream, list);
163         for (idx = 0; idx < 2; idx++) {
164                 subs = &as->substream[idx];
165                 if (!subs->num_formats)
166                         continue;
167                 subs->interface = -1;
168                 subs->data_endpoint = NULL;
169                 subs->sync_endpoint = NULL;
170         }
171 }
172
173 static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
174 {
175         struct usb_device *dev = chip->dev;
176         struct usb_host_interface *alts;
177         struct usb_interface_descriptor *altsd;
178         struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
179
180         if (!iface) {
181                 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
182                            dev->devnum, ctrlif, interface);
183                 return -EINVAL;
184         }
185
186         alts = &iface->altsetting[0];
187         altsd = get_iface_desc(alts);
188
189         /*
190          * Android with both accessory and audio interfaces enabled gets the
191          * interface numbers wrong.
192          */
193         if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
194              chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
195             interface == 0 &&
196             altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
197             altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
198                 interface = 2;
199                 iface = usb_ifnum_to_if(dev, interface);
200                 if (!iface)
201                         return -EINVAL;
202                 alts = &iface->altsetting[0];
203                 altsd = get_iface_desc(alts);
204         }
205
206         if (usb_interface_claimed(iface)) {
207                 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
208                                                 dev->devnum, ctrlif, interface);
209                 return -EINVAL;
210         }
211
212         if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
213              altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
214             altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
215                 int err = snd_usbmidi_create(chip->card, iface,
216                                              &chip->midi_list, NULL);
217                 if (err < 0) {
218                         snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
219                                                 dev->devnum, ctrlif, interface);
220                         return -EINVAL;
221                 }
222                 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
223
224                 return 0;
225         }
226
227         if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
228              altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
229             altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
230                 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
231                                         dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
232                 /* skip non-supported classes */
233                 return -EINVAL;
234         }
235
236         if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
237                 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
238                 return -EINVAL;
239         }
240
241         if (! snd_usb_parse_audio_interface(chip, interface)) {
242                 usb_set_interface(dev, interface, 0); /* reset the current interface */
243                 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
244                 return -EINVAL;
245         }
246
247         return 0;
248 }
249
250 /*
251  * parse audio control descriptor and create pcm/midi streams
252  */
253 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
254 {
255         struct usb_device *dev = chip->dev;
256         struct usb_host_interface *host_iface;
257         struct usb_interface_descriptor *altsd;
258         void *control_header;
259         int i, protocol;
260
261         /* find audiocontrol interface */
262         host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
263         control_header = snd_usb_find_csint_desc(host_iface->extra,
264                                                  host_iface->extralen,
265                                                  NULL, UAC_HEADER);
266         altsd = get_iface_desc(host_iface);
267         protocol = altsd->bInterfaceProtocol;
268
269         if (!control_header) {
270                 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
271                 return -EINVAL;
272         }
273
274         switch (protocol) {
275         default:
276                 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
277                             protocol);
278                 /* fall through */
279
280         case UAC_VERSION_1: {
281                 struct uac1_ac_header_descriptor *h1 = control_header;
282
283                 if (!h1->bInCollection) {
284                         snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
285                         return -EINVAL;
286                 }
287
288                 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
289                         snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
290                         return -EINVAL;
291                 }
292
293                 for (i = 0; i < h1->bInCollection; i++)
294                         snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
295
296                 break;
297         }
298
299         case UAC_VERSION_2: {
300                 struct usb_interface_assoc_descriptor *assoc =
301                         usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
302
303                 if (!assoc) {
304                         /*
305                          * Firmware writers cannot count to three.  So to find
306                          * the IAD on the NuForce UDH-100, also check the next
307                          * interface.
308                          */
309                         struct usb_interface *iface =
310                                 usb_ifnum_to_if(dev, ctrlif + 1);
311                         if (iface &&
312                             iface->intf_assoc &&
313                             iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
314                             iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
315                                 assoc = iface->intf_assoc;
316                 }
317
318                 if (!assoc) {
319                         snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
320                         return -EINVAL;
321                 }
322
323                 for (i = 0; i < assoc->bInterfaceCount; i++) {
324                         int intf = assoc->bFirstInterface + i;
325
326                         if (intf != ctrlif)
327                                 snd_usb_create_stream(chip, ctrlif, intf);
328                 }
329
330                 break;
331         }
332         }
333
334         return 0;
335 }
336
337 /*
338  * free the chip instance
339  *
340  * here we have to do not much, since pcm and controls are already freed
341  *
342  */
343
344 static int snd_usb_audio_free(struct snd_usb_audio *chip)
345 {
346         mutex_destroy(&chip->mutex);
347         kfree(chip);
348         return 0;
349 }
350
351 static int snd_usb_audio_dev_free(struct snd_device *device)
352 {
353         struct snd_usb_audio *chip = device->device_data;
354         return snd_usb_audio_free(chip);
355 }
356
357 static void remove_trailing_spaces(char *str)
358 {
359         char *p;
360
361         if (!*str)
362                 return;
363         for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
364                 *p = 0;
365 }
366
367 /*
368  * create a chip instance and set its names.
369  */
370 static int snd_usb_audio_create(struct usb_device *dev, int idx,
371                                 const struct snd_usb_audio_quirk *quirk,
372                                 struct snd_usb_audio **rchip)
373 {
374         struct snd_card *card;
375         struct snd_usb_audio *chip;
376         int err, len;
377         char component[14];
378         static struct snd_device_ops ops = {
379                 .dev_free =     snd_usb_audio_dev_free,
380         };
381
382         *rchip = NULL;
383
384         switch (snd_usb_get_speed(dev)) {
385         case USB_SPEED_LOW:
386         case USB_SPEED_FULL:
387         case USB_SPEED_HIGH:
388         case USB_SPEED_SUPER:
389                 break;
390         default:
391                 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
392                 return -ENXIO;
393         }
394 #ifdef CONFIG_SND_RK_SOC
395         err = snd_card_create(USB_AUDIO_CARD_NUM, id[idx], THIS_MODULE, 0, &card);
396 #else
397         err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
398 #endif
399         if (err < 0) {
400                 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
401                 return err;
402         }
403
404         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
405         if (! chip) {
406                 snd_card_free(card);
407                 return -ENOMEM;
408         }
409
410         mutex_init(&chip->mutex);
411         init_rwsem(&chip->shutdown_rwsem);
412         chip->index = idx;
413         chip->dev = dev;
414         chip->card = card;
415         chip->setup = device_setup[idx];
416         chip->nrpacks = nrpacks;
417         chip->autoclock = autoclock;
418         chip->probing = 1;
419
420         chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
421                               le16_to_cpu(dev->descriptor.idProduct));
422         INIT_LIST_HEAD(&chip->pcm_list);
423         INIT_LIST_HEAD(&chip->ep_list);
424         INIT_LIST_HEAD(&chip->midi_list);
425         INIT_LIST_HEAD(&chip->mixer_list);
426
427         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
428                 snd_usb_audio_free(chip);
429                 snd_card_free(card);
430                 return err;
431         }
432
433         strcpy(card->driver, "USB-Audio");
434         sprintf(component, "USB%04x:%04x",
435                 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
436         snd_component_add(card, component);
437
438         /* retrieve the device string as shortname */
439         if (quirk && quirk->product_name && *quirk->product_name) {
440                 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
441         } else {
442                 if (!dev->descriptor.iProduct ||
443                     usb_string(dev, dev->descriptor.iProduct,
444                     card->shortname, sizeof(card->shortname)) <= 0) {
445                         /* no name available from anywhere, so use ID */
446                         sprintf(card->shortname, "USB Device %#04x:%#04x",
447                                 USB_ID_VENDOR(chip->usb_id),
448                                 USB_ID_PRODUCT(chip->usb_id));
449                 }
450         }
451         remove_trailing_spaces(card->shortname);
452
453         /* retrieve the vendor and device strings as longname */
454         if (quirk && quirk->vendor_name && *quirk->vendor_name) {
455                 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
456         } else {
457                 if (dev->descriptor.iManufacturer)
458                         len = usb_string(dev, dev->descriptor.iManufacturer,
459                                          card->longname, sizeof(card->longname));
460                 else
461                         len = 0;
462                 /* we don't really care if there isn't any vendor string */
463         }
464         if (len > 0) {
465                 remove_trailing_spaces(card->longname);
466                 if (*card->longname)
467                         strlcat(card->longname, " ", sizeof(card->longname));
468         }
469
470         strlcat(card->longname, card->shortname, sizeof(card->longname));
471
472         len = strlcat(card->longname, " at ", sizeof(card->longname));
473
474         if (len < sizeof(card->longname))
475                 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
476
477         switch (snd_usb_get_speed(dev)) {
478         case USB_SPEED_LOW:
479                 strlcat(card->longname, ", low speed", sizeof(card->longname));
480                 break;
481         case USB_SPEED_FULL:
482                 strlcat(card->longname, ", full speed", sizeof(card->longname));
483                 break;
484         case USB_SPEED_HIGH:
485                 strlcat(card->longname, ", high speed", sizeof(card->longname));
486                 break;
487         case USB_SPEED_SUPER:
488                 strlcat(card->longname, ", super speed", sizeof(card->longname));
489                 break;
490         default:
491                 break;
492         }
493
494         snd_usb_audio_create_proc(chip);
495
496         *rchip = chip;
497         return 0;
498 }
499
500 #ifdef CONFIG_SWITCH
501 static int usb_audio_card_switch_state_update(struct snd_card *card, bool force){
502         struct snd_device *dev;
503     struct snd_pcm *pcm;
504     struct snd_pcm_str *pstr;
505
506         if (snd_BUG_ON(!card))
507                 return -EINVAL;
508         //we use the latest devices
509         list_for_each_entry(dev, &card->devices, list) {
510                 if (dev->type == SNDRV_DEV_PCM && dev->state == SNDRV_DEV_REGISTERED) {
511                         pcm = (struct snd_pcm*)dev->device_data;
512                         if (NULL != pcm) {
513                                 //playback available?
514                                 pstr = &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
515                                 snd_printdd(KERN_INFO "playback substream_count: 0x%x\n", pstr->substream_count);
516                                 if (pstr->substream_count > 0) {
517                                         sUsbaudio_Switch.playback_switch_cur_state = card->number * 10;
518                                 }
519                                 //capture available?
520                                 pstr = &pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
521                                 snd_printdd(KERN_INFO "capture substream_count: 0x%x\n", pstr->substream_count);
522                                 if (pstr->substream_count > 0) {
523                                         sUsbaudio_Switch.capture_switch_cur_state = card->number * 10;
524                                 }
525                         }
526                 }
527         }
528         if (force) {
529                 switch_set_state(&sUsbaudio_Switch.sUsbaudio_Playback, sUsbaudio_Switch.playback_switch_cur_state);
530                 switch_set_state(&sUsbaudio_Switch.sUsbaudio_Capture, sUsbaudio_Switch.capture_switch_cur_state);
531         }
532         return 0;
533
534 }
535
536 static void usb_audio_switch_state_update_all(void)
537 {
538         int index = 0;
539         struct snd_card *card;
540
541         sUsbaudio_Switch.playback_switch_cur_state = NO_USBAUDIO_PLAYBACK;
542         sUsbaudio_Switch.capture_switch_cur_state = NO_USBAUDIO_CAPTURE;
543
544         for (index = 0; index < SNDRV_CARDS; ++index) {
545                 if (NULL != usb_chip[index]) {
546                         card = usb_chip[index]->card;
547                         usb_audio_card_switch_state_update(card, false);
548                 }
549         }
550
551         switch_set_state(&sUsbaudio_Switch.sUsbaudio_Playback,
552                          sUsbaudio_Switch.playback_switch_cur_state);
553         switch_set_state(&sUsbaudio_Switch.sUsbaudio_Capture,
554                          sUsbaudio_Switch.capture_switch_cur_state);
555 }
556 #endif
557
558
559 /*
560  * probe the active usb device
561  *
562  * note that this can be called multiple times per a device, when it
563  * includes multiple audio control interfaces.
564  *
565  * thus we check the usb device pointer and creates the card instance
566  * only at the first time.  the successive calls of this function will
567  * append the pcm interface to the corresponding card.
568  */
569 static struct snd_usb_audio *
570 snd_usb_audio_probe(struct usb_device *dev,
571                     struct usb_interface *intf,
572                     const struct usb_device_id *usb_id)
573 {
574         const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
575         int i, err;
576         struct snd_usb_audio *chip;
577         struct usb_host_interface *alts;
578         int ifnum;
579         u32 id;
580
581         alts = &intf->altsetting[0];
582         ifnum = get_iface_desc(alts)->bInterfaceNumber;
583         id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
584                     le16_to_cpu(dev->descriptor.idProduct));
585         if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
586                 goto __err_val;
587
588         if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
589                 goto __err_val;
590
591         /*
592          * found a config.  now register to ALSA
593          */
594
595         /* check whether it's already registered */
596         chip = NULL;
597         mutex_lock(&register_mutex);
598         for (i = 0; i < SNDRV_CARDS; i++) {
599                 if (usb_chip[i] && usb_chip[i]->dev == dev) {
600                         if (usb_chip[i]->shutdown) {
601                                 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
602                                 goto __error;
603                         }
604                         chip = usb_chip[i];
605                         chip->probing = 1;
606                         break;
607                 }
608         }
609         if (! chip) {
610                 /* it's a fresh one.
611                  * now look for an empty slot and create a new card instance
612                  */
613                 // use RBASE_USB_AUDIO_IDX instead of zero, modify by zxg.
614                 for (i = 0; i < SNDRV_CARDS; i++)
615                         if (enable[i] && ! usb_chip[i] &&
616                             (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
617                             (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
618                                 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
619                                         goto __error;
620                                 }
621                                 snd_card_set_dev(chip->card, &intf->dev);
622                                 chip->pm_intf = intf;
623                                 break;
624                         }
625                 if (!chip) {
626                         printk(KERN_ERR "no available usb audio device\n");
627                         goto __error;
628                 }
629         }
630
631         /*
632          * For devices with more than one control interface, we assume the
633          * first contains the audio controls. We might need a more specific
634          * check here in the future.
635          */
636         if (!chip->ctrl_intf)
637                 chip->ctrl_intf = alts;
638
639         chip->txfr_quirk = 0;
640         err = 1; /* continue */
641         if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
642                 /* need some special handlings */
643                 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
644                         goto __error;
645         }
646
647         if (err > 0) {
648                 /* create normal USB audio interfaces */
649                 if (snd_usb_create_streams(chip, ifnum) < 0 ||
650                     snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
651                         goto __error;
652                 }
653         }
654
655         /* we are allowed to call snd_card_register() many times */
656         if (snd_card_register(chip->card) < 0) {
657                 goto __error;
658         }
659 #ifdef CONFIG_SWITCH
660         if (usb_audio_card_switch_state_update(chip->card, true) < 0) {
661                 printk(KERN_ERR "usb_audio_card_switch_state_update failed!!!\n");
662         goto __error;
663         }
664 #endif
665         usb_chip[chip->index] = chip;
666         chip->num_interfaces++;
667         chip->probing = 0;
668         mutex_unlock(&register_mutex);
669         return chip;
670
671  __error:
672         if (chip) {
673                 if (!chip->num_interfaces)
674                         snd_card_free(chip->card);
675                 chip->probing = 0;
676         }
677         mutex_unlock(&register_mutex);
678  __err_val:
679         return NULL;
680 }
681
682 /*
683  * we need to take care of counter, since disconnection can be called also
684  * many times as well as usb_audio_probe().
685  */
686 static void snd_usb_audio_disconnect(struct usb_device *dev,
687                                      struct snd_usb_audio *chip)
688 {
689         struct snd_card *card;
690         struct list_head *p, *n;
691
692         if (chip == (void *)-1L)
693                 return;
694
695         card = chip->card;
696         down_write(&chip->shutdown_rwsem);
697         chip->shutdown = 1;
698         up_write(&chip->shutdown_rwsem);
699
700         mutex_lock(&register_mutex);
701         chip->num_interfaces--;
702         if (chip->num_interfaces <= 0) {
703                 snd_card_disconnect(card);
704                 /* release the pcm resources */
705                 list_for_each(p, &chip->pcm_list) {
706                         snd_usb_stream_disconnect(p);
707                 }
708                 /* release the endpoint resources */
709                 list_for_each_safe(p, n, &chip->ep_list) {
710                         snd_usb_endpoint_free(p);
711                 }
712                 /* release the midi resources */
713                 list_for_each(p, &chip->midi_list) {
714                         snd_usbmidi_disconnect(p);
715                 }
716                 /* release mixer resources */
717                 list_for_each(p, &chip->mixer_list) {
718                         snd_usb_mixer_disconnect(p);
719                 }
720                 usb_chip[chip->index] = NULL;
721                 mutex_unlock(&register_mutex);
722                 snd_card_free_when_closed(card);
723         } else {
724                 mutex_unlock(&register_mutex);
725         }
726 #ifdef CONFIG_SWITCH
727         usb_audio_switch_state_update_all();
728 #endif
729 }
730
731 /*
732  * new 2.5 USB kernel API
733  */
734 static int usb_audio_probe(struct usb_interface *intf,
735                            const struct usb_device_id *id)
736 {
737         struct snd_usb_audio *chip;
738         chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
739         if (chip) {
740                 usb_set_intfdata(intf, chip);
741 #ifdef CONFIG_SND_RK_SOC
742                 switch_set_state(usb_audio_sdev, 1);
743 #endif
744                 return 0;
745         } else
746                 return -EIO;
747 }
748
749 static void usb_audio_disconnect(struct usb_interface *intf)
750 {
751 #ifdef CONFIG_SND_RK_SOC
752         switch_set_state(usb_audio_sdev, 0);
753 #endif
754         snd_usb_audio_disconnect(interface_to_usbdev(intf),
755                                  usb_get_intfdata(intf));
756 }
757
758 #ifdef CONFIG_PM
759
760 int snd_usb_autoresume(struct snd_usb_audio *chip)
761 {
762         int err = -ENODEV;
763
764         down_read(&chip->shutdown_rwsem);
765         if (chip->probing)
766                 err = 0;
767         else if (!chip->shutdown)
768                 err = usb_autopm_get_interface(chip->pm_intf);
769         up_read(&chip->shutdown_rwsem);
770
771         return err;
772 }
773
774 void snd_usb_autosuspend(struct snd_usb_audio *chip)
775 {
776         down_read(&chip->shutdown_rwsem);
777         if (!chip->shutdown && !chip->probing)
778                 usb_autopm_put_interface(chip->pm_intf);
779         up_read(&chip->shutdown_rwsem);
780 }
781
782 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
783 {
784         struct snd_usb_audio *chip = usb_get_intfdata(intf);
785         struct snd_usb_stream *as;
786         struct usb_mixer_interface *mixer;
787
788         if (chip == (void *)-1L)
789                 return 0;
790
791         if (!PMSG_IS_AUTO(message)) {
792                 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
793                 if (!chip->num_suspended_intf++) {
794                         list_for_each_entry(as, &chip->pcm_list, list) {
795                                 snd_pcm_suspend_all(as->pcm);
796                                 as->substream[0].need_setup_ep =
797                                         as->substream[1].need_setup_ep = true;
798                         }
799                 }
800         } else {
801                 /*
802                  * otherwise we keep the rest of the system in the dark
803                  * to keep this transparent
804                  */
805                 if (!chip->num_suspended_intf++)
806                         chip->autosuspended = 1;
807         }
808
809         list_for_each_entry(mixer, &chip->mixer_list, list)
810                 snd_usb_mixer_inactivate(mixer);
811
812         return 0;
813 }
814
815 static int usb_audio_resume(struct usb_interface *intf)
816 {
817         struct snd_usb_audio *chip = usb_get_intfdata(intf);
818         struct usb_mixer_interface *mixer;
819         int err = 0;
820
821         if (chip == (void *)-1L)
822                 return 0;
823         if (--chip->num_suspended_intf)
824                 return 0;
825         /*
826          * ALSA leaves material resumption to user space
827          * we just notify and restart the mixers
828          */
829         list_for_each_entry(mixer, &chip->mixer_list, list) {
830                 err = snd_usb_mixer_activate(mixer);
831                 if (err < 0)
832                         goto err_out;
833         }
834
835         if (!chip->autosuspended)
836                 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
837         chip->autosuspended = 0;
838
839 err_out:
840         return err;
841 }
842 #else
843 #define usb_audio_suspend       NULL
844 #define usb_audio_resume        NULL
845 #endif          /* CONFIG_PM */
846
847 static struct usb_device_id usb_audio_ids [] = {
848 #include "quirks-table.h"
849     { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
850       .bInterfaceClass = USB_CLASS_AUDIO,
851       .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
852     { }                                         /* Terminating entry */
853 };
854 MODULE_DEVICE_TABLE(usb, usb_audio_ids);
855
856 /*
857  * entry point for linux usb interface
858  */
859
860 static struct usb_driver usb_audio_driver = {
861         .name =         "snd-usb-audio",
862         .probe =        usb_audio_probe,
863         .disconnect =   usb_audio_disconnect,
864         .suspend =      usb_audio_suspend,
865         .resume =       usb_audio_resume,
866         .id_table =     usb_audio_ids,
867         .supports_autosuspend = 1,
868 };
869
870 static int __init snd_usb_audio_init(void)
871 {
872         int ret;
873 #ifdef CONFIG_SWITCH
874         int i;
875         ret = switch_dev_register(&sUsbaudio_Switch.sUsbaudio_Playback);
876         if (ret)
877                 goto err;
878         ret = switch_dev_register(&sUsbaudio_Switch.sUsbaudio_Capture);
879         if (ret)
880                 goto err_drv;
881
882         switch_set_state(&sUsbaudio_Switch.sUsbaudio_Playback, NO_USBAUDIO_PLAYBACK);
883         switch_set_state(&sUsbaudio_Switch.sUsbaudio_Capture, NO_USBAUDIO_CAPTURE);
884 #endif
885
886         if (nrpacks < 1 || nrpacks > MAX_PACKS) {
887                 printk(KERN_WARNING "invalid nrpacks value.\n");
888                 return -EINVAL;
889         }
890 #ifdef CONFIG_SND_RK_SOC
891         usb_audio_sdev = kzalloc(sizeof(usb_audio_sdev), GFP_KERNEL);
892         usb_audio_sdev->name = "usb_audio";
893         switch_dev_register(usb_audio_sdev);
894         if (!usb_audio_sdev) {
895                 printk(KERN_WARNING "usb-audio kmalloc fail!");
896                 return -ENOMEM;
897         }
898 #endif
899
900     //re initial array index for rebasing usb audio cards create, modify by zxg.
901     for(i=0; i<SNDRV_CARDS; ++i){
902         index[i] = i;
903     }
904
905         return usb_register(&usb_audio_driver);
906
907 err_drv:
908 #ifdef CONFIG_SWITCH
909         switch_dev_unregister(&sUsbaudio_Switch.sUsbaudio_Playback);
910 #endif
911 err:
912     return ret;
913
914 }
915
916 static void __exit snd_usb_audio_cleanup(void)
917 {
918         usb_deregister(&usb_audio_driver);
919 #ifdef CONFIG_SND_RK_SOC
920         kfree(usb_audio_sdev);
921 #endif
922         switch_dev_unregister(&sUsbaudio_Switch.sUsbaudio_Capture);
923         switch_dev_unregister(&sUsbaudio_Switch.sUsbaudio_Playback);
924 }
925
926 /*module_init(snd_usb_audio_init);*/
927 /* use late initcall_sync instead of module_init,
928  * make sure that usbaudio probe after board codec.
929  * added by zxg@rock-chips.com
930  */
931 late_initcall_sync(snd_usb_audio_init);
932 module_exit(snd_usb_audio_cleanup);