···2222 will be called snd-usb-audio.23232424config SND_USB_UA1012525- tristate "Edirol UA-101/UA-1000 driver (EXPERIMENTAL)"2626- depends on EXPERIMENTAL2525+ tristate "Edirol UA-101/UA-1000 driver"2726 select SND_PCM2827 select SND_RAWMIDI2928 help···6465 * Native Instruments Audio 8 DJ6566 * Native Instruments Guitar Rig Session I/O6667 * Native Instruments Guitar Rig mobile6868+ * Native Instruments Traktor Kontrol X167696870 To compile this driver as a module, choose M here: the module6971 will be called snd-usb-caiaq.
···11+/*22+ * (Tentative) USB Audio Driver for ALSA33+ *44+ * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>55+ *66+ * Many codes borrowed from audio.c by77+ * Alan Cox (alan@lxorguk.ukuu.org.uk)88+ * Thomas Sailer (sailer@ife.ee.ethz.ch)99+ *1010+ *1111+ * This program is free software; you can redistribute it and/or modify1212+ * it under the terms of the GNU General Public License as published by1313+ * the Free Software Foundation; either version 2 of the License, or1414+ * (at your option) any later version.1515+ *1616+ * This program is distributed in the hope that it will be useful,1717+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1919+ * GNU General Public License for more details.2020+ *2121+ * You should have received a copy of the GNU General Public License2222+ * along with this program; if not, write to the Free Software2323+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA2424+ *2525+ *2626+ * NOTES:2727+ *2828+ * - async unlink should be used for avoiding the sleep inside lock.2929+ * 2.4.22 usb-uhci seems buggy for async unlinking and results in3030+ * oops. in such a cse, pass async_unlink=0 option.3131+ * - the linked URBs would be preferred but not used so far because of3232+ * the instability of unlinking.3333+ * - type II is not supported properly. there is no device which supports3434+ * this type *correctly*. SB extigy looks as if it supports, but it's3535+ * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).3636+ */3737+3838+3939+#include <linux/bitops.h>4040+#include <linux/init.h>4141+#include <linux/list.h>4242+#include <linux/slab.h>4343+#include <linux/string.h>4444+#include <linux/usb.h>4545+#include <linux/moduleparam.h>4646+#include <linux/mutex.h>4747+#include <linux/usb/audio.h>4848+#include <linux/usb/audio-v2.h>4949+5050+#include <sound/core.h>5151+#include <sound/info.h>5252+#include <sound/pcm.h>5353+#include <sound/pcm_params.h>5454+#include <sound/initval.h>5555+5656+#include "usbaudio.h"5757+#include "card.h"5858+#include "midi.h"5959+#include "mixer.h"6060+#include "proc.h"6161+#include "quirks.h"6262+#include "endpoint.h"6363+#include "helper.h"6464+#include "debug.h"6565+#include "pcm.h"6666+#include "urb.h"6767+#include "format.h"6868+6969+MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");7070+MODULE_DESCRIPTION("USB Audio");7171+MODULE_LICENSE("GPL");7272+MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");7373+7474+7575+static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */7676+static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */7777+static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */7878+/* Vendor/product IDs for this card */7979+static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };8080+static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };8181+static int nrpacks = 8; /* max. number of packets per urb */8282+static int async_unlink = 1;8383+static int device_setup[SNDRV_CARDS]; /* device parameter for this card */8484+static int ignore_ctl_error;8585+8686+module_param_array(index, int, NULL, 0444);8787+MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");8888+module_param_array(id, charp, NULL, 0444);8989+MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");9090+module_param_array(enable, bool, NULL, 0444);9191+MODULE_PARM_DESC(enable, "Enable USB audio adapter.");9292+module_param_array(vid, int, NULL, 0444);9393+MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");9494+module_param_array(pid, int, NULL, 0444);9595+MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");9696+module_param(nrpacks, int, 0644);9797+MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");9898+module_param(async_unlink, bool, 0444);9999+MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");100100+module_param_array(device_setup, int, NULL, 0444);101101+MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");102102+module_param(ignore_ctl_error, bool, 0444);103103+MODULE_PARM_DESC(ignore_ctl_error,104104+ "Ignore errors from USB controller for mixer interfaces.");105105+106106+/*107107+ * we keep the snd_usb_audio_t instances by ourselves for merging108108+ * the all interfaces on the same card as one sound device.109109+ */110110+111111+static DEFINE_MUTEX(register_mutex);112112+static struct snd_usb_audio *usb_chip[SNDRV_CARDS];113113+static struct usb_driver usb_audio_driver;114114+115115+/*116116+ * disconnect streams117117+ * called from snd_usb_audio_disconnect()118118+ */119119+static void snd_usb_stream_disconnect(struct list_head *head)120120+{121121+ int idx;122122+ struct snd_usb_stream *as;123123+ struct snd_usb_substream *subs;124124+125125+ as = list_entry(head, struct snd_usb_stream, list);126126+ for (idx = 0; idx < 2; idx++) {127127+ subs = &as->substream[idx];128128+ if (!subs->num_formats)129129+ return;130130+ snd_usb_release_substream_urbs(subs, 1);131131+ subs->interface = -1;132132+ }133133+}134134+135135+static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)136136+{137137+ struct usb_device *dev = chip->dev;138138+ struct usb_host_interface *alts;139139+ struct usb_interface_descriptor *altsd;140140+ struct usb_interface *iface = usb_ifnum_to_if(dev, interface);141141+142142+ if (!iface) {143143+ snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",144144+ dev->devnum, ctrlif, interface);145145+ return -EINVAL;146146+ }147147+148148+ if (usb_interface_claimed(iface)) {149149+ snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",150150+ dev->devnum, ctrlif, interface);151151+ return -EINVAL;152152+ }153153+154154+ alts = &iface->altsetting[0];155155+ altsd = get_iface_desc(alts);156156+ if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||157157+ altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&158158+ altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {159159+ int err = snd_usbmidi_create(chip->card, iface,160160+ &chip->midi_list, NULL);161161+ if (err < 0) {162162+ snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",163163+ dev->devnum, ctrlif, interface);164164+ return -EINVAL;165165+ }166166+ usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);167167+168168+ return 0;169169+ }170170+171171+ if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&172172+ altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||173173+ altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {174174+ snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",175175+ dev->devnum, ctrlif, interface, altsd->bInterfaceClass);176176+ /* skip non-supported classes */177177+ return -EINVAL;178178+ }179179+180180+ if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {181181+ snd_printk(KERN_ERR "low speed audio streaming not supported\n");182182+ return -EINVAL;183183+ }184184+185185+ if (! snd_usb_parse_audio_endpoints(chip, interface)) {186186+ usb_set_interface(dev, interface, 0); /* reset the current interface */187187+ usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);188188+ return -EINVAL;189189+ }190190+191191+ return 0;192192+}193193+194194+/*195195+ * parse audio control descriptor and create pcm/midi streams196196+ */197197+static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)198198+{199199+ struct usb_device *dev = chip->dev;200200+ struct usb_host_interface *host_iface;201201+ struct usb_interface_descriptor *altsd;202202+ void *control_header;203203+ int i, protocol;204204+205205+ /* find audiocontrol interface */206206+ host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];207207+ control_header = snd_usb_find_csint_desc(host_iface->extra,208208+ host_iface->extralen,209209+ NULL, UAC_HEADER);210210+ altsd = get_iface_desc(host_iface);211211+ protocol = altsd->bInterfaceProtocol;212212+213213+ if (!control_header) {214214+ snd_printk(KERN_ERR "cannot find UAC_HEADER\n");215215+ return -EINVAL;216216+ }217217+218218+ switch (protocol) {219219+ case UAC_VERSION_1: {220220+ struct uac_ac_header_descriptor_v1 *h1 = control_header;221221+222222+ if (!h1->bInCollection) {223223+ snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");224224+ return -EINVAL;225225+ }226226+227227+ if (h1->bLength < sizeof(*h1) + h1->bInCollection) {228228+ snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");229229+ return -EINVAL;230230+ }231231+232232+ for (i = 0; i < h1->bInCollection; i++)233233+ snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);234234+235235+ break;236236+ }237237+238238+ case UAC_VERSION_2: {239239+ struct uac_clock_source_descriptor *cs;240240+ struct usb_interface_assoc_descriptor *assoc =241241+ usb_ifnum_to_if(dev, ctrlif)->intf_assoc;242242+243243+ if (!assoc) {244244+ snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");245245+ return -EINVAL;246246+ }247247+248248+ /* FIXME: for now, we expect there is at least one clock source249249+ * descriptor and we always take the first one.250250+ * We should properly support devices with multiple clock sources,251251+ * clock selectors and sample rate conversion units. */252252+253253+ cs = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen,254254+ NULL, UAC2_CLOCK_SOURCE);255255+256256+ if (!cs) {257257+ snd_printk(KERN_ERR "CLOCK_SOURCE descriptor not found\n");258258+ return -EINVAL;259259+ }260260+261261+ chip->clock_id = cs->bClockID;262262+263263+ for (i = 0; i < assoc->bInterfaceCount; i++) {264264+ int intf = assoc->bFirstInterface + i;265265+266266+ if (intf != ctrlif)267267+ snd_usb_create_stream(chip, ctrlif, intf);268268+ }269269+270270+ break;271271+ }272272+273273+ default:274274+ snd_printk(KERN_ERR "unknown protocol version 0x%02x\n", protocol);275275+ return -EINVAL;276276+ }277277+278278+ return 0;279279+}280280+281281+/*282282+ * free the chip instance283283+ *284284+ * here we have to do not much, since pcm and controls are already freed285285+ *286286+ */287287+288288+static int snd_usb_audio_free(struct snd_usb_audio *chip)289289+{290290+ kfree(chip);291291+ return 0;292292+}293293+294294+static int snd_usb_audio_dev_free(struct snd_device *device)295295+{296296+ struct snd_usb_audio *chip = device->device_data;297297+ return snd_usb_audio_free(chip);298298+}299299+300300+301301+/*302302+ * create a chip instance and set its names.303303+ */304304+static int snd_usb_audio_create(struct usb_device *dev, int idx,305305+ const struct snd_usb_audio_quirk *quirk,306306+ struct snd_usb_audio **rchip)307307+{308308+ struct snd_card *card;309309+ struct snd_usb_audio *chip;310310+ int err, len;311311+ char component[14];312312+ static struct snd_device_ops ops = {313313+ .dev_free = snd_usb_audio_dev_free,314314+ };315315+316316+ *rchip = NULL;317317+318318+ if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&319319+ snd_usb_get_speed(dev) != USB_SPEED_FULL &&320320+ snd_usb_get_speed(dev) != USB_SPEED_HIGH) {321321+ snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));322322+ return -ENXIO;323323+ }324324+325325+ err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);326326+ if (err < 0) {327327+ snd_printk(KERN_ERR "cannot create card instance %d\n", idx);328328+ return err;329329+ }330330+331331+ chip = kzalloc(sizeof(*chip), GFP_KERNEL);332332+ if (! chip) {333333+ snd_card_free(card);334334+ return -ENOMEM;335335+ }336336+337337+ chip->index = idx;338338+ chip->dev = dev;339339+ chip->card = card;340340+ chip->setup = device_setup[idx];341341+ chip->nrpacks = nrpacks;342342+ chip->async_unlink = async_unlink;343343+344344+ chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),345345+ le16_to_cpu(dev->descriptor.idProduct));346346+ INIT_LIST_HEAD(&chip->pcm_list);347347+ INIT_LIST_HEAD(&chip->midi_list);348348+ INIT_LIST_HEAD(&chip->mixer_list);349349+350350+ if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {351351+ snd_usb_audio_free(chip);352352+ snd_card_free(card);353353+ return err;354354+ }355355+356356+ strcpy(card->driver, "USB-Audio");357357+ sprintf(component, "USB%04x:%04x",358358+ USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));359359+ snd_component_add(card, component);360360+361361+ /* retrieve the device string as shortname */362362+ if (quirk && quirk->product_name) {363363+ strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));364364+ } else {365365+ if (!dev->descriptor.iProduct ||366366+ usb_string(dev, dev->descriptor.iProduct,367367+ card->shortname, sizeof(card->shortname)) <= 0) {368368+ /* no name available from anywhere, so use ID */369369+ sprintf(card->shortname, "USB Device %#04x:%#04x",370370+ USB_ID_VENDOR(chip->usb_id),371371+ USB_ID_PRODUCT(chip->usb_id));372372+ }373373+ }374374+375375+ /* retrieve the vendor and device strings as longname */376376+ if (quirk && quirk->vendor_name) {377377+ len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));378378+ } else {379379+ if (dev->descriptor.iManufacturer)380380+ len = usb_string(dev, dev->descriptor.iManufacturer,381381+ card->longname, sizeof(card->longname));382382+ else383383+ len = 0;384384+ /* we don't really care if there isn't any vendor string */385385+ }386386+ if (len > 0)387387+ strlcat(card->longname, " ", sizeof(card->longname));388388+389389+ strlcat(card->longname, card->shortname, sizeof(card->longname));390390+391391+ len = strlcat(card->longname, " at ", sizeof(card->longname));392392+393393+ if (len < sizeof(card->longname))394394+ usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);395395+396396+ strlcat(card->longname,397397+ snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :398398+ snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :399399+ ", high speed",400400+ sizeof(card->longname));401401+402402+ snd_usb_audio_create_proc(chip);403403+404404+ *rchip = chip;405405+ return 0;406406+}407407+408408+/*409409+ * probe the active usb device410410+ *411411+ * note that this can be called multiple times per a device, when it412412+ * includes multiple audio control interfaces.413413+ *414414+ * thus we check the usb device pointer and creates the card instance415415+ * only at the first time. the successive calls of this function will416416+ * append the pcm interface to the corresponding card.417417+ */418418+static void *snd_usb_audio_probe(struct usb_device *dev,419419+ struct usb_interface *intf,420420+ const struct usb_device_id *usb_id)421421+{422422+ const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;423423+ int i, err;424424+ struct snd_usb_audio *chip;425425+ struct usb_host_interface *alts;426426+ int ifnum;427427+ u32 id;428428+429429+ alts = &intf->altsetting[0];430430+ ifnum = get_iface_desc(alts)->bInterfaceNumber;431431+ id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),432432+ le16_to_cpu(dev->descriptor.idProduct));433433+ if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)434434+ goto __err_val;435435+436436+ if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)437437+ goto __err_val;438438+439439+ /*440440+ * found a config. now register to ALSA441441+ */442442+443443+ /* check whether it's already registered */444444+ chip = NULL;445445+ mutex_lock(®ister_mutex);446446+ for (i = 0; i < SNDRV_CARDS; i++) {447447+ if (usb_chip[i] && usb_chip[i]->dev == dev) {448448+ if (usb_chip[i]->shutdown) {449449+ snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");450450+ goto __error;451451+ }452452+ chip = usb_chip[i];453453+ break;454454+ }455455+ }456456+ if (! chip) {457457+ /* it's a fresh one.458458+ * now look for an empty slot and create a new card instance459459+ */460460+ for (i = 0; i < SNDRV_CARDS; i++)461461+ if (enable[i] && ! usb_chip[i] &&462462+ (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&463463+ (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {464464+ if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {465465+ goto __error;466466+ }467467+ snd_card_set_dev(chip->card, &intf->dev);468468+ break;469469+ }470470+ if (!chip) {471471+ printk(KERN_ERR "no available usb audio device\n");472472+ goto __error;473473+ }474474+ }475475+476476+ chip->txfr_quirk = 0;477477+ err = 1; /* continue */478478+ if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {479479+ /* need some special handlings */480480+ if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)481481+ goto __error;482482+ }483483+484484+ if (err > 0) {485485+ /* create normal USB audio interfaces */486486+ if (snd_usb_create_streams(chip, ifnum) < 0 ||487487+ snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {488488+ goto __error;489489+ }490490+ }491491+492492+ /* we are allowed to call snd_card_register() many times */493493+ if (snd_card_register(chip->card) < 0) {494494+ goto __error;495495+ }496496+497497+ usb_chip[chip->index] = chip;498498+ chip->num_interfaces++;499499+ mutex_unlock(®ister_mutex);500500+ return chip;501501+502502+ __error:503503+ if (chip && !chip->num_interfaces)504504+ snd_card_free(chip->card);505505+ mutex_unlock(®ister_mutex);506506+ __err_val:507507+ return NULL;508508+}509509+510510+/*511511+ * we need to take care of counter, since disconnection can be called also512512+ * many times as well as usb_audio_probe().513513+ */514514+static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)515515+{516516+ struct snd_usb_audio *chip;517517+ struct snd_card *card;518518+ struct list_head *p;519519+520520+ if (ptr == (void *)-1L)521521+ return;522522+523523+ chip = ptr;524524+ card = chip->card;525525+ mutex_lock(®ister_mutex);526526+ chip->shutdown = 1;527527+ chip->num_interfaces--;528528+ if (chip->num_interfaces <= 0) {529529+ snd_card_disconnect(card);530530+ /* release the pcm resources */531531+ list_for_each(p, &chip->pcm_list) {532532+ snd_usb_stream_disconnect(p);533533+ }534534+ /* release the midi resources */535535+ list_for_each(p, &chip->midi_list) {536536+ snd_usbmidi_disconnect(p);537537+ }538538+ /* release mixer resources */539539+ list_for_each(p, &chip->mixer_list) {540540+ snd_usb_mixer_disconnect(p);541541+ }542542+ usb_chip[chip->index] = NULL;543543+ mutex_unlock(®ister_mutex);544544+ snd_card_free_when_closed(card);545545+ } else {546546+ mutex_unlock(®ister_mutex);547547+ }548548+}549549+550550+/*551551+ * new 2.5 USB kernel API552552+ */553553+static int usb_audio_probe(struct usb_interface *intf,554554+ const struct usb_device_id *id)555555+{556556+ void *chip;557557+ chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);558558+ if (chip) {559559+ usb_set_intfdata(intf, chip);560560+ return 0;561561+ } else562562+ return -EIO;563563+}564564+565565+static void usb_audio_disconnect(struct usb_interface *intf)566566+{567567+ snd_usb_audio_disconnect(interface_to_usbdev(intf),568568+ usb_get_intfdata(intf));569569+}570570+571571+#ifdef CONFIG_PM572572+static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)573573+{574574+ struct snd_usb_audio *chip = usb_get_intfdata(intf);575575+ struct list_head *p;576576+ struct snd_usb_stream *as;577577+578578+ if (chip == (void *)-1L)579579+ return 0;580580+581581+ snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);582582+ if (!chip->num_suspended_intf++) {583583+ list_for_each(p, &chip->pcm_list) {584584+ as = list_entry(p, struct snd_usb_stream, list);585585+ snd_pcm_suspend_all(as->pcm);586586+ }587587+ }588588+589589+ return 0;590590+}591591+592592+static int usb_audio_resume(struct usb_interface *intf)593593+{594594+ struct snd_usb_audio *chip = usb_get_intfdata(intf);595595+596596+ if (chip == (void *)-1L)597597+ return 0;598598+ if (--chip->num_suspended_intf)599599+ return 0;600600+ /*601601+ * ALSA leaves material resumption to user space602602+ * we just notify603603+ */604604+605605+ snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);606606+607607+ return 0;608608+}609609+#else610610+#define usb_audio_suspend NULL611611+#define usb_audio_resume NULL612612+#endif /* CONFIG_PM */613613+614614+static struct usb_device_id usb_audio_ids [] = {615615+#include "quirks-table.h"616616+ { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),617617+ .bInterfaceClass = USB_CLASS_AUDIO,618618+ .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },619619+ { } /* Terminating entry */620620+};621621+622622+MODULE_DEVICE_TABLE (usb, usb_audio_ids);623623+624624+/*625625+ * entry point for linux usb interface626626+ */627627+628628+static struct usb_driver usb_audio_driver = {629629+ .name = "snd-usb-audio",630630+ .probe = usb_audio_probe,631631+ .disconnect = usb_audio_disconnect,632632+ .suspend = usb_audio_suspend,633633+ .resume = usb_audio_resume,634634+ .id_table = usb_audio_ids,635635+};636636+637637+static int __init snd_usb_audio_init(void)638638+{639639+ if (nrpacks < 1 || nrpacks > MAX_PACKS) {640640+ printk(KERN_WARNING "invalid nrpacks value.\n");641641+ return -EINVAL;642642+ }643643+ return usb_register(&usb_audio_driver);644644+}645645+646646+static void __exit snd_usb_audio_cleanup(void)647647+{648648+ usb_deregister(&usb_audio_driver);649649+}650650+651651+module_init(snd_usb_audio_init);652652+module_exit(snd_usb_audio_cleanup);
+105
sound/usb/card.h
···11+#ifndef __USBAUDIO_CARD_H22+#define __USBAUDIO_CARD_H33+44+#define MAX_PACKS 2055+#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */66+#define MAX_URBS 877+#define SYNC_URBS 4 /* always four urbs for sync */88+#define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */99+1010+struct audioformat {1111+ struct list_head list;1212+ u64 formats; /* ALSA format bits */1313+ unsigned int channels; /* # channels */1414+ unsigned int fmt_type; /* USB audio format type (1-3) */1515+ unsigned int frame_size; /* samples per frame for non-audio */1616+ int iface; /* interface number */1717+ unsigned char altsetting; /* corresponding alternate setting */1818+ unsigned char altset_idx; /* array index of altenate setting */1919+ unsigned char attributes; /* corresponding attributes of cs endpoint */2020+ unsigned char endpoint; /* endpoint */2121+ unsigned char ep_attr; /* endpoint attributes */2222+ unsigned char datainterval; /* log_2 of data packet interval */2323+ unsigned int maxpacksize; /* max. packet size */2424+ unsigned int rates; /* rate bitmasks */2525+ unsigned int rate_min, rate_max; /* min/max rates */2626+ unsigned int nr_rates; /* number of rate table entries */2727+ unsigned int *rate_table; /* rate table */2828+};2929+3030+struct snd_usb_substream;3131+3232+struct snd_urb_ctx {3333+ struct urb *urb;3434+ unsigned int buffer_size; /* size of data buffer, if data URB */3535+ struct snd_usb_substream *subs;3636+ int index; /* index for urb array */3737+ int packets; /* number of packets per urb */3838+};3939+4040+struct snd_urb_ops {4141+ int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4242+ int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4343+ int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4444+ int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4545+};4646+4747+struct snd_usb_substream {4848+ struct snd_usb_stream *stream;4949+ struct usb_device *dev;5050+ struct snd_pcm_substream *pcm_substream;5151+ int direction; /* playback or capture */5252+ int interface; /* current interface */5353+ int endpoint; /* assigned endpoint */5454+ struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */5555+ unsigned int cur_rate; /* current rate (for hw_params callback) */5656+ unsigned int period_bytes; /* current period bytes (for hw_params callback) */5757+ unsigned int altset_idx; /* USB data format: index of alternate setting */5858+ unsigned int datapipe; /* the data i/o pipe */5959+ unsigned int syncpipe; /* 1 - async out or adaptive in */6060+ unsigned int datainterval; /* log_2 of data packet interval */6161+ unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */6262+ unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */6363+ unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */6464+ unsigned int freqmax; /* maximum sampling rate, used for buffer management */6565+ unsigned int phase; /* phase accumulator */6666+ unsigned int maxpacksize; /* max packet size in bytes */6767+ unsigned int maxframesize; /* max packet size in frames */6868+ unsigned int curpacksize; /* current packet size in bytes (for capture) */6969+ unsigned int curframesize; /* current packet size in frames (for capture) */7070+ unsigned int fill_max: 1; /* fill max packet size always */7171+ unsigned int txfr_quirk:1; /* allow sub-frame alignment */7272+ unsigned int fmt_type; /* USB audio format type (1-3) */7373+7474+ unsigned int running: 1; /* running status */7575+7676+ unsigned int hwptr_done; /* processed byte position in the buffer */7777+ unsigned int transfer_done; /* processed frames since last period update */7878+ unsigned long active_mask; /* bitmask of active urbs */7979+ unsigned long unlink_mask; /* bitmask of unlinked urbs */8080+8181+ unsigned int nurbs; /* # urbs */8282+ struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */8383+ struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */8484+ char *syncbuf; /* sync buffer for all sync URBs */8585+ dma_addr_t sync_dma; /* DMA address of syncbuf */8686+8787+ u64 formats; /* format bitmasks (all or'ed) */8888+ unsigned int num_formats; /* number of supported audio formats (list) */8989+ struct list_head fmt_list; /* format list */9090+ struct snd_pcm_hw_constraint_list rate_list; /* limited rates */9191+ spinlock_t lock;9292+9393+ struct snd_urb_ops ops; /* callbacks (must be filled at init) */9494+};9595+9696+struct snd_usb_stream {9797+ struct snd_usb_audio *chip;9898+ struct snd_pcm *pcm;9999+ int pcm_index;100100+ unsigned int fmt_type; /* USB audio format type (1-3) */101101+ struct snd_usb_substream substream[2];102102+ struct list_head list;103103+};104104+105105+#endif /* __USBAUDIO_CARD_H */
···11+/*22+ * This program is free software; you can redistribute it and/or modify33+ * it under the terms of the GNU General Public License as published by44+ * the Free Software Foundation; either version 2 of the License, or55+ * (at your option) any later version.66+ *77+ * This program is distributed in the hope that it will be useful,88+ * but WITHOUT ANY WARRANTY; without even the implied warranty of99+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1010+ * GNU General Public License for more details.1111+ *1212+ * You should have received a copy of the GNU General Public License1313+ * along with this program; if not, write to the Free Software1414+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA1515+ *1616+ */1717+1818+#include <linux/init.h>1919+#include <linux/slab.h>2020+#include <linux/usb.h>2121+#include <linux/usb/audio.h>2222+#include <linux/usb/audio-v2.h>2323+2424+#include <sound/core.h>2525+#include <sound/pcm.h>2626+2727+#include "usbaudio.h"2828+#include "card.h"2929+#include "proc.h"3030+#include "quirks.h"3131+#include "endpoint.h"3232+#include "urb.h"3333+#include "pcm.h"3434+#include "helper.h"3535+#include "format.h"3636+3737+/*3838+ * free a substream3939+ */4040+static void free_substream(struct snd_usb_substream *subs)4141+{4242+ struct list_head *p, *n;4343+4444+ if (!subs->num_formats)4545+ return; /* not initialized */4646+ list_for_each_safe(p, n, &subs->fmt_list) {4747+ struct audioformat *fp = list_entry(p, struct audioformat, list);4848+ kfree(fp->rate_table);4949+ kfree(fp);5050+ }5151+ kfree(subs->rate_list.list);5252+}5353+5454+5555+/*5656+ * free a usb stream instance5757+ */5858+static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)5959+{6060+ free_substream(&stream->substream[0]);6161+ free_substream(&stream->substream[1]);6262+ list_del(&stream->list);6363+ kfree(stream);6464+}6565+6666+static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)6767+{6868+ struct snd_usb_stream *stream = pcm->private_data;6969+ if (stream) {7070+ stream->pcm = NULL;7171+ snd_usb_audio_stream_free(stream);7272+ }7373+}7474+7575+7676+/*7777+ * add this endpoint to the chip instance.7878+ * if a stream with the same endpoint already exists, append to it.7979+ * if not, create a new pcm stream.8080+ */8181+int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)8282+{8383+ struct list_head *p;8484+ struct snd_usb_stream *as;8585+ struct snd_usb_substream *subs;8686+ struct snd_pcm *pcm;8787+ int err;8888+8989+ list_for_each(p, &chip->pcm_list) {9090+ as = list_entry(p, struct snd_usb_stream, list);9191+ if (as->fmt_type != fp->fmt_type)9292+ continue;9393+ subs = &as->substream[stream];9494+ if (!subs->endpoint)9595+ continue;9696+ if (subs->endpoint == fp->endpoint) {9797+ list_add_tail(&fp->list, &subs->fmt_list);9898+ subs->num_formats++;9999+ subs->formats |= fp->formats;100100+ return 0;101101+ }102102+ }103103+ /* look for an empty stream */104104+ list_for_each(p, &chip->pcm_list) {105105+ as = list_entry(p, struct snd_usb_stream, list);106106+ if (as->fmt_type != fp->fmt_type)107107+ continue;108108+ subs = &as->substream[stream];109109+ if (subs->endpoint)110110+ continue;111111+ err = snd_pcm_new_stream(as->pcm, stream, 1);112112+ if (err < 0)113113+ return err;114114+ snd_usb_init_substream(as, stream, fp);115115+ return 0;116116+ }117117+118118+ /* create a new pcm */119119+ as = kzalloc(sizeof(*as), GFP_KERNEL);120120+ if (!as)121121+ return -ENOMEM;122122+ as->pcm_index = chip->pcm_devs;123123+ as->chip = chip;124124+ as->fmt_type = fp->fmt_type;125125+ err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,126126+ stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,127127+ stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,128128+ &pcm);129129+ if (err < 0) {130130+ kfree(as);131131+ return err;132132+ }133133+ as->pcm = pcm;134134+ pcm->private_data = as;135135+ pcm->private_free = snd_usb_audio_pcm_free;136136+ pcm->info_flags = 0;137137+ if (chip->pcm_devs > 0)138138+ sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);139139+ else140140+ strcpy(pcm->name, "USB Audio");141141+142142+ snd_usb_init_substream(as, stream, fp);143143+144144+ list_add(&as->list, &chip->pcm_list);145145+ chip->pcm_devs++;146146+147147+ snd_usb_proc_pcm_format_add(as);148148+149149+ return 0;150150+}151151+152152+int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)153153+{154154+ struct usb_device *dev;155155+ struct usb_interface *iface;156156+ struct usb_host_interface *alts;157157+ struct usb_interface_descriptor *altsd;158158+ int i, altno, err, stream;159159+ int format = 0, num_channels = 0;160160+ struct audioformat *fp = NULL;161161+ unsigned char *fmt, *csep;162162+ int num, protocol;163163+164164+ dev = chip->dev;165165+166166+ /* parse the interface's altsettings */167167+ iface = usb_ifnum_to_if(dev, iface_no);168168+169169+ num = iface->num_altsetting;170170+171171+ /*172172+ * Dallas DS4201 workaround: It presents 5 altsettings, but the last173173+ * one misses syncpipe, and does not produce any sound.174174+ */175175+ if (chip->usb_id == USB_ID(0x04fa, 0x4201))176176+ num = 4;177177+178178+ for (i = 0; i < num; i++) {179179+ alts = &iface->altsetting[i];180180+ altsd = get_iface_desc(alts);181181+ protocol = altsd->bInterfaceProtocol;182182+ /* skip invalid one */183183+ if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&184184+ altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||185185+ (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&186186+ altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||187187+ altsd->bNumEndpoints < 1 ||188188+ le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)189189+ continue;190190+ /* must be isochronous */191191+ if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=192192+ USB_ENDPOINT_XFER_ISOC)193193+ continue;194194+ /* check direction */195195+ stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?196196+ SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;197197+ altno = altsd->bAlternateSetting;198198+199199+ if (snd_usb_apply_interface_quirk(chip, iface_no, altno))200200+ continue;201201+202202+ /* get audio formats */203203+ switch (protocol) {204204+ case UAC_VERSION_1: {205205+ struct uac_as_header_descriptor_v1 *as =206206+ snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);207207+208208+ if (!as) {209209+ snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",210210+ dev->devnum, iface_no, altno);211211+ continue;212212+ }213213+214214+ if (as->bLength < sizeof(*as)) {215215+ snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",216216+ dev->devnum, iface_no, altno);217217+ continue;218218+ }219219+220220+ format = le16_to_cpu(as->wFormatTag); /* remember the format value */221221+ break;222222+ }223223+224224+ case UAC_VERSION_2: {225225+ struct uac_as_header_descriptor_v2 *as =226226+ snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);227227+228228+ if (!as) {229229+ snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",230230+ dev->devnum, iface_no, altno);231231+ continue;232232+ }233233+234234+ if (as->bLength < sizeof(*as)) {235235+ snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",236236+ dev->devnum, iface_no, altno);237237+ continue;238238+ }239239+240240+ num_channels = as->bNrChannels;241241+ format = le32_to_cpu(as->bmFormats);242242+243243+ break;244244+ }245245+246246+ default:247247+ snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",248248+ dev->devnum, iface_no, altno, protocol);249249+ continue;250250+ }251251+252252+ /* get format type */253253+ fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);254254+ if (!fmt) {255255+ snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",256256+ dev->devnum, iface_no, altno);257257+ continue;258258+ }259259+ if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||260260+ ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {261261+ snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",262262+ dev->devnum, iface_no, altno);263263+ continue;264264+ }265265+266266+ /*267267+ * Blue Microphones workaround: The last altsetting is identical268268+ * with the previous one, except for a larger packet size, but269269+ * is actually a mislabeled two-channel setting; ignore it.270270+ */271271+ if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&272272+ fp && fp->altsetting == 1 && fp->channels == 1 &&273273+ fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&274274+ protocol == UAC_VERSION_1 &&275275+ le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==276276+ fp->maxpacksize * 2)277277+ continue;278278+279279+ csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);280280+ /* Creamware Noah has this descriptor after the 2nd endpoint */281281+ if (!csep && altsd->bNumEndpoints >= 2)282282+ csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);283283+ if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {284284+ snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"285285+ " class specific endpoint descriptor\n",286286+ dev->devnum, iface_no, altno);287287+ csep = NULL;288288+ }289289+290290+ fp = kzalloc(sizeof(*fp), GFP_KERNEL);291291+ if (! fp) {292292+ snd_printk(KERN_ERR "cannot malloc\n");293293+ return -ENOMEM;294294+ }295295+296296+ fp->iface = iface_no;297297+ fp->altsetting = altno;298298+ fp->altset_idx = i;299299+ fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;300300+ fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;301301+ fp->datainterval = snd_usb_parse_datainterval(chip, alts);302302+ fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);303303+ /* num_channels is only set for v2 interfaces */304304+ fp->channels = num_channels;305305+ if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)306306+ fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)307307+ * (fp->maxpacksize & 0x7ff);308308+ fp->attributes = csep ? csep[3] : 0;309309+310310+ /* some quirks for attributes here */311311+312312+ switch (chip->usb_id) {313313+ case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */314314+ /* Optoplay sets the sample rate attribute although315315+ * it seems not supporting it in fact.316316+ */317317+ fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;318318+ break;319319+ case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */320320+ case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */321321+ case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra 8 */322322+ case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */323323+ /* doesn't set the sample rate attribute, but supports it */324324+ fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;325325+ break;326326+ case USB_ID(0x047f, 0x0ca1): /* plantronics headset */327327+ case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is328328+ an older model 77d:223) */329329+ /*330330+ * plantronics headset and Griffin iMic have set adaptive-in331331+ * although it's really not...332332+ */333333+ fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;334334+ if (stream == SNDRV_PCM_STREAM_PLAYBACK)335335+ fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;336336+ else337337+ fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;338338+ break;339339+ }340340+341341+ /* ok, let's parse further... */342342+ if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {343343+ kfree(fp->rate_table);344344+ kfree(fp);345345+ continue;346346+ }347347+348348+ snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);349349+ err = snd_usb_add_audio_endpoint(chip, stream, fp);350350+ if (err < 0) {351351+ kfree(fp->rate_table);352352+ kfree(fp);353353+ return err;354354+ }355355+ /* try to set the interface... */356356+ usb_set_interface(chip->dev, iface_no, altno);357357+ snd_usb_init_pitch(chip, iface_no, alts, fp);358358+ snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);359359+ }360360+ return 0;361361+}362362+
+11
sound/usb/endpoint.h
···11+#ifndef __USBAUDIO_ENDPOINT_H22+#define __USBAUDIO_ENDPOINT_H33+44+int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip,55+ int iface_no);66+77+int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip,88+ int stream,99+ struct audioformat *fp);1010+1111+#endif /* __USBAUDIO_ENDPOINT_H */
+432
sound/usb/format.c
···11+/*22+ * This program is free software; you can redistribute it and/or modify33+ * it under the terms of the GNU General Public License as published by44+ * the Free Software Foundation; either version 2 of the License, or55+ * (at your option) any later version.66+ *77+ * This program is distributed in the hope that it will be useful,88+ * but WITHOUT ANY WARRANTY; without even the implied warranty of99+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1010+ * GNU General Public License for more details.1111+ *1212+ * You should have received a copy of the GNU General Public License1313+ * along with this program; if not, write to the Free Software1414+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA1515+ *1616+ */1717+1818+#include <linux/init.h>1919+#include <linux/slab.h>2020+#include <linux/usb.h>2121+#include <linux/usb/audio.h>2222+#include <linux/usb/audio-v2.h>2323+2424+#include <sound/core.h>2525+#include <sound/pcm.h>2626+2727+#include "usbaudio.h"2828+#include "card.h"2929+#include "quirks.h"3030+#include "helper.h"3131+#include "debug.h"3232+3333+/*3434+ * parse the audio format type I descriptor3535+ * and returns the corresponding pcm format3636+ *3737+ * @dev: usb device3838+ * @fp: audioformat record3939+ * @format: the format tag (wFormatTag)4040+ * @fmt: the format type descriptor4141+ */4242+static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,4343+ struct audioformat *fp,4444+ int format, void *_fmt,4545+ int protocol)4646+{4747+ int sample_width, sample_bytes;4848+ u64 pcm_formats;4949+5050+ switch (protocol) {5151+ case UAC_VERSION_1: {5252+ struct uac_format_type_i_discrete_descriptor *fmt = _fmt;5353+ sample_width = fmt->bBitResolution;5454+ sample_bytes = fmt->bSubframeSize;5555+ format = 1 << format;5656+ break;5757+ }5858+5959+ case UAC_VERSION_2: {6060+ struct uac_format_type_i_ext_descriptor *fmt = _fmt;6161+ sample_width = fmt->bBitResolution;6262+ sample_bytes = fmt->bSubslotSize;6363+ format <<= 1;6464+ break;6565+ }6666+6767+ default:6868+ return -EINVAL;6969+ }7070+7171+ pcm_formats = 0;7272+7373+ if (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED)) {7474+ /* some devices don't define this correctly... */7575+ snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",7676+ chip->dev->devnum, fp->iface, fp->altsetting);7777+ format = 1 << UAC_FORMAT_TYPE_I_PCM;7878+ }7979+ if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {8080+ if (sample_width > sample_bytes * 8) {8181+ snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",8282+ chip->dev->devnum, fp->iface, fp->altsetting,8383+ sample_width, sample_bytes);8484+ }8585+ /* check the format byte size */8686+ switch (sample_bytes) {8787+ case 1:8888+ pcm_formats |= SNDRV_PCM_FMTBIT_S8;8989+ break;9090+ case 2:9191+ if (snd_usb_is_big_endian_format(chip, fp))9292+ pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */9393+ else9494+ pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;9595+ break;9696+ case 3:9797+ if (snd_usb_is_big_endian_format(chip, fp))9898+ pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */9999+ else100100+ pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;101101+ break;102102+ case 4:103103+ pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;104104+ break;105105+ default:106106+ snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",107107+ chip->dev->devnum, fp->iface, fp->altsetting,108108+ sample_width, sample_bytes);109109+ break;110110+ }111111+ }112112+ if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {113113+ /* Dallas DS4201 workaround: it advertises U8 format, but really114114+ supports S8. */115115+ if (chip->usb_id == USB_ID(0x04fa, 0x4201))116116+ pcm_formats |= SNDRV_PCM_FMTBIT_S8;117117+ else118118+ pcm_formats |= SNDRV_PCM_FMTBIT_U8;119119+ }120120+ if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {121121+ pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;122122+ }123123+ if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {124124+ pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;125125+ }126126+ if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {127127+ pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;128128+ }129129+ if (format & ~0x3f) {130130+ snd_printk(KERN_INFO "%d:%u:%d : unsupported format bits %#x\n",131131+ chip->dev->devnum, fp->iface, fp->altsetting, format);132132+ }133133+ return pcm_formats;134134+}135135+136136+137137+/*138138+ * parse the format descriptor and stores the possible sample rates139139+ * on the audioformat table (audio class v1).140140+ *141141+ * @dev: usb device142142+ * @fp: audioformat record143143+ * @fmt: the format descriptor144144+ * @offset: the start offset of descriptor pointing the rate type145145+ * (7 for type I and II, 8 for type II)146146+ */147147+static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,148148+ unsigned char *fmt, int offset)149149+{150150+ int nr_rates = fmt[offset];151151+152152+ if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {153153+ snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",154154+ chip->dev->devnum, fp->iface, fp->altsetting);155155+ return -1;156156+ }157157+158158+ if (nr_rates) {159159+ /*160160+ * build the rate table and bitmap flags161161+ */162162+ int r, idx;163163+164164+ fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);165165+ if (fp->rate_table == NULL) {166166+ snd_printk(KERN_ERR "cannot malloc\n");167167+ return -1;168168+ }169169+170170+ fp->nr_rates = 0;171171+ fp->rate_min = fp->rate_max = 0;172172+ for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {173173+ unsigned int rate = combine_triple(&fmt[idx]);174174+ if (!rate)175175+ continue;176176+ /* C-Media CM6501 mislabels its 96 kHz altsetting */177177+ if (rate == 48000 && nr_rates == 1 &&178178+ (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||179179+ chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&180180+ fp->altsetting == 5 && fp->maxpacksize == 392)181181+ rate = 96000;182182+ /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */183183+ if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))184184+ rate = 8000;185185+186186+ fp->rate_table[fp->nr_rates] = rate;187187+ if (!fp->rate_min || rate < fp->rate_min)188188+ fp->rate_min = rate;189189+ if (!fp->rate_max || rate > fp->rate_max)190190+ fp->rate_max = rate;191191+ fp->rates |= snd_pcm_rate_to_rate_bit(rate);192192+ fp->nr_rates++;193193+ }194194+ if (!fp->nr_rates) {195195+ hwc_debug("All rates were zero. Skipping format!\n");196196+ return -1;197197+ }198198+ } else {199199+ /* continuous rates */200200+ fp->rates = SNDRV_PCM_RATE_CONTINUOUS;201201+ fp->rate_min = combine_triple(&fmt[offset + 1]);202202+ fp->rate_max = combine_triple(&fmt[offset + 4]);203203+ }204204+ return 0;205205+}206206+207207+/*208208+ * parse the format descriptor and stores the possible sample rates209209+ * on the audioformat table (audio class v2).210210+ */211211+static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,212212+ struct audioformat *fp,213213+ struct usb_host_interface *iface)214214+{215215+ struct usb_device *dev = chip->dev;216216+ unsigned char tmp[2], *data;217217+ int i, nr_rates, data_size, ret = 0;218218+219219+ /* get the number of sample rates first by only fetching 2 bytes */220220+ ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,221221+ USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,222222+ UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,223223+ tmp, sizeof(tmp), 1000);224224+225225+ if (ret < 0) {226226+ snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");227227+ goto err;228228+ }229229+230230+ nr_rates = (tmp[1] << 8) | tmp[0];231231+ data_size = 2 + 12 * nr_rates;232232+ data = kzalloc(data_size, GFP_KERNEL);233233+ if (!data) {234234+ ret = -ENOMEM;235235+ goto err;236236+ }237237+238238+ /* now get the full information */239239+ ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,240240+ USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,241241+ UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,242242+ data, data_size, 1000);243243+244244+ if (ret < 0) {245245+ snd_printk(KERN_ERR "unable to retrieve sample rate range\n");246246+ ret = -EINVAL;247247+ goto err_free;248248+ }249249+250250+ fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);251251+ if (!fp->rate_table) {252252+ ret = -ENOMEM;253253+ goto err_free;254254+ }255255+256256+ fp->nr_rates = 0;257257+ fp->rate_min = fp->rate_max = 0;258258+259259+ for (i = 0; i < nr_rates; i++) {260260+ int rate = combine_quad(&data[2 + 12 * i]);261261+262262+ fp->rate_table[fp->nr_rates] = rate;263263+ if (!fp->rate_min || rate < fp->rate_min)264264+ fp->rate_min = rate;265265+ if (!fp->rate_max || rate > fp->rate_max)266266+ fp->rate_max = rate;267267+ fp->rates |= snd_pcm_rate_to_rate_bit(rate);268268+ fp->nr_rates++;269269+ }270270+271271+err_free:272272+ kfree(data);273273+err:274274+ return ret;275275+}276276+277277+/*278278+ * parse the format type I and III descriptors279279+ */280280+static int parse_audio_format_i(struct snd_usb_audio *chip,281281+ struct audioformat *fp,282282+ int format, void *_fmt,283283+ struct usb_host_interface *iface)284284+{285285+ struct usb_interface_descriptor *altsd = get_iface_desc(iface);286286+ struct uac_format_type_i_discrete_descriptor *fmt = _fmt;287287+ int protocol = altsd->bInterfaceProtocol;288288+ int pcm_format, ret;289289+290290+ if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {291291+ /* FIXME: the format type is really IECxxx292292+ * but we give normal PCM format to get the existing293293+ * apps working...294294+ */295295+ switch (chip->usb_id) {296296+297297+ case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */298298+ if (chip->setup == 0x00 && 299299+ fp->altsetting == 6)300300+ pcm_format = SNDRV_PCM_FORMAT_S16_BE;301301+ else302302+ pcm_format = SNDRV_PCM_FORMAT_S16_LE;303303+ break;304304+ default:305305+ pcm_format = SNDRV_PCM_FORMAT_S16_LE;306306+ }307307+ fp->formats = 1uLL << pcm_format;308308+ } else {309309+ fp->formats = parse_audio_format_i_type(chip, fp, format,310310+ fmt, protocol);311311+ if (!fp->formats)312312+ return -1;313313+ }314314+315315+ /* gather possible sample rates */316316+ /* audio class v1 reports possible sample rates as part of the317317+ * proprietary class specific descriptor.318318+ * audio class v2 uses class specific EP0 range requests for that.319319+ */320320+ switch (protocol) {321321+ case UAC_VERSION_1:322322+ fp->channels = fmt->bNrChannels;323323+ ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);324324+ break;325325+ case UAC_VERSION_2:326326+ /* fp->channels is already set in this case */327327+ ret = parse_audio_format_rates_v2(chip, fp, iface);328328+ break;329329+ }330330+331331+ if (fp->channels < 1) {332332+ snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",333333+ chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);334334+ return -1;335335+ }336336+337337+ return ret;338338+}339339+340340+/*341341+ * parse the format type II descriptor342342+ */343343+static int parse_audio_format_ii(struct snd_usb_audio *chip,344344+ struct audioformat *fp,345345+ int format, void *_fmt,346346+ struct usb_host_interface *iface)347347+{348348+ int brate, framesize, ret;349349+ struct usb_interface_descriptor *altsd = get_iface_desc(iface);350350+ int protocol = altsd->bInterfaceProtocol;351351+352352+ switch (format) {353353+ case UAC_FORMAT_TYPE_II_AC3:354354+ /* FIXME: there is no AC3 format defined yet */355355+ // fp->formats = SNDRV_PCM_FMTBIT_AC3;356356+ fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */357357+ break;358358+ case UAC_FORMAT_TYPE_II_MPEG:359359+ fp->formats = SNDRV_PCM_FMTBIT_MPEG;360360+ break;361361+ default:362362+ snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",363363+ chip->dev->devnum, fp->iface, fp->altsetting, format);364364+ fp->formats = SNDRV_PCM_FMTBIT_MPEG;365365+ break;366366+ }367367+368368+ fp->channels = 1;369369+370370+ switch (protocol) {371371+ case UAC_VERSION_1: {372372+ struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;373373+ brate = le16_to_cpu(fmt->wMaxBitRate);374374+ framesize = le16_to_cpu(fmt->wSamplesPerFrame);375375+ snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);376376+ fp->frame_size = framesize;377377+ ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */378378+ break;379379+ }380380+ case UAC_VERSION_2: {381381+ struct uac_format_type_ii_ext_descriptor *fmt = _fmt;382382+ brate = le16_to_cpu(fmt->wMaxBitRate);383383+ framesize = le16_to_cpu(fmt->wSamplesPerFrame);384384+ snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);385385+ fp->frame_size = framesize;386386+ ret = parse_audio_format_rates_v2(chip, fp, iface);387387+ break;388388+ }389389+ }390390+391391+ return ret;392392+}393393+394394+int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,395395+ int format, unsigned char *fmt, int stream,396396+ struct usb_host_interface *iface)397397+{398398+ int err;399399+400400+ switch (fmt[3]) {401401+ case UAC_FORMAT_TYPE_I:402402+ case UAC_FORMAT_TYPE_III:403403+ err = parse_audio_format_i(chip, fp, format, fmt, iface);404404+ break;405405+ case UAC_FORMAT_TYPE_II:406406+ err = parse_audio_format_ii(chip, fp, format, fmt, iface);407407+ break;408408+ default:409409+ snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",410410+ chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);411411+ return -1;412412+ }413413+ fp->fmt_type = fmt[3];414414+ if (err < 0)415415+ return err;416416+#if 1417417+ /* FIXME: temporary hack for extigy/audigy 2 nx/zs */418418+ /* extigy apparently supports sample rates other than 48k419419+ * but not in ordinary way. so we enable only 48k atm.420420+ */421421+ if (chip->usb_id == USB_ID(0x041e, 0x3000) ||422422+ chip->usb_id == USB_ID(0x041e, 0x3020) ||423423+ chip->usb_id == USB_ID(0x041e, 0x3061)) {424424+ if (fmt[3] == UAC_FORMAT_TYPE_I &&425425+ fp->rates != SNDRV_PCM_RATE_48000 &&426426+ fp->rates != SNDRV_PCM_RATE_96000)427427+ return -1;428428+ }429429+#endif430430+ return 0;431431+}432432+
+8
sound/usb/format.h
···11+#ifndef __USBAUDIO_FORMAT_H22+#define __USBAUDIO_FORMAT_H33+44+int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,55+ int format, unsigned char *fmt, int stream,66+ struct usb_host_interface *iface);77+88+#endif /* __USBAUDIO_FORMAT_H */
+113
sound/usb/helper.c
···11+/*22+ * This program is free software; you can redistribute it and/or modify33+ * it under the terms of the GNU General Public License as published by44+ * the Free Software Foundation; either version 2 of the License, or55+ * (at your option) any later version.66+ *77+ * This program is distributed in the hope that it will be useful,88+ * but WITHOUT ANY WARRANTY; without even the implied warranty of99+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1010+ * GNU General Public License for more details.1111+ *1212+ * You should have received a copy of the GNU General Public License1313+ * along with this program; if not, write to the Free Software1414+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA1515+ *1616+ */1717+1818+#include <linux/init.h>1919+#include <linux/slab.h>2020+#include <linux/usb.h>2121+2222+#include "usbaudio.h"2323+#include "helper.h"2424+2525+/*2626+ * combine bytes and get an integer value2727+ */2828+unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)2929+{3030+ switch (size) {3131+ case 1: return *bytes;3232+ case 2: return combine_word(bytes);3333+ case 3: return combine_triple(bytes);3434+ case 4: return combine_quad(bytes);3535+ default: return 0;3636+ }3737+}3838+3939+/*4040+ * parse descriptor buffer and return the pointer starting the given4141+ * descriptor type.4242+ */4343+void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)4444+{4545+ u8 *p, *end, *next;4646+4747+ p = descstart;4848+ end = p + desclen;4949+ for (; p < end;) {5050+ if (p[0] < 2)5151+ return NULL;5252+ next = p + p[0];5353+ if (next > end)5454+ return NULL;5555+ if (p[1] == dtype && (!after || (void *)p > after)) {5656+ return p;5757+ }5858+ p = next;5959+ }6060+ return NULL;6161+}6262+6363+/*6464+ * find a class-specified interface descriptor with the given subtype.6565+ */6666+void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)6767+{6868+ unsigned char *p = after;6969+7070+ while ((p = snd_usb_find_desc(buffer, buflen, p,7171+ USB_DT_CS_INTERFACE)) != NULL) {7272+ if (p[0] >= 3 && p[2] == dsubtype)7373+ return p;7474+ }7575+ return NULL;7676+}7777+7878+/*7979+ * Wrapper for usb_control_msg().8080+ * Allocates a temp buffer to prevent dmaing from/to the stack.8181+ */8282+int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,8383+ __u8 requesttype, __u16 value, __u16 index, void *data,8484+ __u16 size, int timeout)8585+{8686+ int err;8787+ void *buf = NULL;8888+8989+ if (size > 0) {9090+ buf = kmemdup(data, size, GFP_KERNEL);9191+ if (!buf)9292+ return -ENOMEM;9393+ }9494+ err = usb_control_msg(dev, pipe, request, requesttype,9595+ value, index, buf, size, timeout);9696+ if (size > 0) {9797+ memcpy(data, buf, size);9898+ kfree(buf);9999+ }100100+ return err;101101+}102102+103103+unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,104104+ struct usb_host_interface *alts)105105+{106106+ if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&107107+ get_endpoint(alts, 0)->bInterval >= 1 &&108108+ get_endpoint(alts, 0)->bInterval <= 4)109109+ return get_endpoint(alts, 0)->bInterval - 1;110110+ else111111+ return 0;112112+}113113+
+32
sound/usb/helper.h
···11+#ifndef __USBAUDIO_HELPER_H22+#define __USBAUDIO_HELPER_H33+44+unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size);55+66+void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype);77+void *snd_usb_find_csint_desc(void *descstart, int desclen, void *after, u8 dsubtype);88+99+int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe,1010+ __u8 request, __u8 requesttype, __u16 value, __u16 index,1111+ void *data, __u16 size, int timeout);1212+1313+unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,1414+ struct usb_host_interface *alts);1515+1616+/*1717+ * retrieve usb_interface descriptor from the host interface1818+ * (conditional for compatibility with the older API)1919+ */2020+#ifndef get_iface_desc2121+#define get_iface_desc(iface) (&(iface)->desc)2222+#define get_endpoint(alt,ep) (&(alt)->endpoint[ep].desc)2323+#define get_ep_desc(ep) (&(ep)->desc)2424+#define get_cfg_desc(cfg) (&(cfg)->desc)2525+#endif2626+2727+#ifndef snd_usb_get_speed2828+#define snd_usb_get_speed(dev) ((dev)->speed)2929+#endif3030+3131+3232+#endif /* __USBAUDIO_HELPER_H */
+48
sound/usb/midi.h
···11+#ifndef __USBMIDI_H22+#define __USBMIDI_H33+44+/* maximum number of endpoints per interface */55+#define MIDI_MAX_ENDPOINTS 266+77+/* data for QUIRK_MIDI_FIXED_ENDPOINT */88+struct snd_usb_midi_endpoint_info {99+ int8_t out_ep; /* ep number, 0 autodetect */1010+ uint8_t out_interval; /* interval for interrupt endpoints */1111+ int8_t in_ep;1212+ uint8_t in_interval;1313+ uint16_t out_cables; /* bitmask */1414+ uint16_t in_cables; /* bitmask */1515+};1616+1717+/* for QUIRK_MIDI_YAMAHA, data is NULL */1818+1919+/* for QUIRK_MIDI_MIDIMAN, data points to a snd_usb_midi_endpoint_info2020+ * structure (out_cables and in_cables only) */2121+2222+/* for QUIRK_COMPOSITE, data points to an array of snd_usb_audio_quirk2323+ * structures, terminated with .ifnum = -1 */2424+2525+/* for QUIRK_AUDIO_FIXED_ENDPOINT, data points to an audioformat structure */2626+2727+/* for QUIRK_AUDIO/MIDI_STANDARD_INTERFACE, data is NULL */2828+2929+/* for QUIRK_AUDIO_EDIROL_UA700_UA25/UA1000, data is NULL */3030+3131+/* for QUIRK_IGNORE_INTERFACE, data is NULL */3232+3333+/* for QUIRK_MIDI_NOVATION and _RAW, data is NULL */3434+3535+/* for QUIRK_MIDI_EMAGIC, data points to a snd_usb_midi_endpoint_info3636+ * structure (out_cables and in_cables only) */3737+3838+/* for QUIRK_MIDI_CME, data is NULL */3939+4040+int snd_usbmidi_create(struct snd_card *card,4141+ struct usb_interface *iface,4242+ struct list_head *midi_list,4343+ const struct snd_usb_audio_quirk *quirk);4444+void snd_usbmidi_input_stop(struct list_head* p);4545+void snd_usbmidi_input_start(struct list_head* p);4646+void snd_usbmidi_disconnect(struct list_head *p);4747+4848+#endif /* __USBMIDI_H */
···11+#ifndef __USBMIXER_H22+#define __USBMIXER_H33+44+struct usb_mixer_interface {55+ struct snd_usb_audio *chip;66+ unsigned int ctrlif;77+ struct list_head list;88+ unsigned int ignore_ctl_error;99+ struct urb *urb;1010+ /* array[MAX_ID_ELEMS], indexed by unit id */1111+ struct usb_mixer_elem_info **id_elems;1212+1313+ /* the usb audio specification version this interface complies to */1414+ int protocol;1515+1616+ /* Sound Blaster remote control stuff */1717+ const struct rc_config *rc_cfg;1818+ u32 rc_code;1919+ wait_queue_head_t rc_waitq;2020+ struct urb *rc_urb;2121+ struct usb_ctrlrequest *rc_setup_packet;2222+ u8 rc_buffer[6];2323+2424+ u8 audigy2nx_leds[3];2525+ u8 xonar_u1_status;2626+};2727+2828+#define MAX_CHANNELS 10 /* max logical channels */2929+3030+struct usb_mixer_elem_info {3131+ struct usb_mixer_interface *mixer;3232+ struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */3333+ struct snd_ctl_elem_id *elem_id;3434+ unsigned int id;3535+ unsigned int control; /* CS or ICN (high byte) */3636+ unsigned int cmask; /* channel mask bitmap: 0 = master */3737+ int channels;3838+ int val_type;3939+ int min, max, res;4040+ int dBmin, dBmax;4141+ int cached;4242+ int cache_val[MAX_CHANNELS];4343+ u8 initialized;4444+};4545+4646+int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,4747+ int ignore_error);4848+void snd_usb_mixer_disconnect(struct list_head *p);4949+5050+void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid);5151+5252+int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,5353+ int request, int validx, int value_set);5454+5555+#endif /* __USBMIXER_H */
+412
sound/usb/mixer_quirks.c
···11+/*22+ * USB Audio Driver for ALSA33+ *44+ * Quirks and vendor-specific extensions for mixer interfaces55+ *66+ * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>77+ *88+ * Many codes borrowed from audio.c by99+ * Alan Cox (alan@lxorguk.ukuu.org.uk)1010+ * Thomas Sailer (sailer@ife.ee.ethz.ch)1111+ *1212+ *1313+ * This program is free software; you can redistribute it and/or modify1414+ * it under the terms of the GNU General Public License as published by1515+ * the Free Software Foundation; either version 2 of the License, or1616+ * (at your option) any later version.1717+ *1818+ * This program is distributed in the hope that it will be useful,1919+ * but WITHOUT ANY WARRANTY; without even the implied warranty of2020+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the2121+ * GNU General Public License for more details.2222+ *2323+ * You should have received a copy of the GNU General Public License2424+ * along with this program; if not, write to the Free Software2525+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA2626+ */2727+2828+#include <linux/init.h>2929+#include <linux/slab.h>3030+#include <linux/usb.h>3131+#include <linux/usb/audio.h>3232+3333+#include <sound/core.h>3434+#include <sound/control.h>3535+#include <sound/hwdep.h>3636+#include <sound/info.h>3737+3838+#include "usbaudio.h"3939+#include "mixer.h"4040+#include "mixer_quirks.h"4141+#include "helper.h"4242+4343+/*4444+ * Sound Blaster remote control configuration4545+ *4646+ * format of remote control data:4747+ * Extigy: xx 004848+ * Audigy 2 NX: 06 80 xx 00 00 004949+ * Live! 24-bit: 06 80 xx yy 22 835050+ */5151+static const struct rc_config {5252+ u32 usb_id;5353+ u8 offset;5454+ u8 length;5555+ u8 packet_length;5656+ u8 min_packet_length; /* minimum accepted length of the URB result */5757+ u8 mute_mixer_id;5858+ u32 mute_code;5959+} rc_configs[] = {6060+ { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */6161+ { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */6262+ { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */6363+ { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */6464+};6565+6666+static void snd_usb_soundblaster_remote_complete(struct urb *urb)6767+{6868+ struct usb_mixer_interface *mixer = urb->context;6969+ const struct rc_config *rc = mixer->rc_cfg;7070+ u32 code;7171+7272+ if (urb->status < 0 || urb->actual_length < rc->min_packet_length)7373+ return;7474+7575+ code = mixer->rc_buffer[rc->offset];7676+ if (rc->length == 2)7777+ code |= mixer->rc_buffer[rc->offset + 1] << 8;7878+7979+ /* the Mute button actually changes the mixer control */8080+ if (code == rc->mute_code)8181+ snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);8282+ mixer->rc_code = code;8383+ wmb();8484+ wake_up(&mixer->rc_waitq);8585+}8686+8787+static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,8888+ long count, loff_t *offset)8989+{9090+ struct usb_mixer_interface *mixer = hw->private_data;9191+ int err;9292+ u32 rc_code;9393+9494+ if (count != 1 && count != 4)9595+ return -EINVAL;9696+ err = wait_event_interruptible(mixer->rc_waitq,9797+ (rc_code = xchg(&mixer->rc_code, 0)) != 0);9898+ if (err == 0) {9999+ if (count == 1)100100+ err = put_user(rc_code, buf);101101+ else102102+ err = put_user(rc_code, (u32 __user *)buf);103103+ }104104+ return err < 0 ? err : count;105105+}106106+107107+static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,108108+ poll_table *wait)109109+{110110+ struct usb_mixer_interface *mixer = hw->private_data;111111+112112+ poll_wait(file, &mixer->rc_waitq, wait);113113+ return mixer->rc_code ? POLLIN | POLLRDNORM : 0;114114+}115115+116116+static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)117117+{118118+ struct snd_hwdep *hwdep;119119+ int err, len, i;120120+121121+ for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)122122+ if (rc_configs[i].usb_id == mixer->chip->usb_id)123123+ break;124124+ if (i >= ARRAY_SIZE(rc_configs))125125+ return 0;126126+ mixer->rc_cfg = &rc_configs[i];127127+128128+ len = mixer->rc_cfg->packet_length;129129+130130+ init_waitqueue_head(&mixer->rc_waitq);131131+ err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);132132+ if (err < 0)133133+ return err;134134+ snprintf(hwdep->name, sizeof(hwdep->name),135135+ "%s remote control", mixer->chip->card->shortname);136136+ hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;137137+ hwdep->private_data = mixer;138138+ hwdep->ops.read = snd_usb_sbrc_hwdep_read;139139+ hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;140140+ hwdep->exclusive = 1;141141+142142+ mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);143143+ if (!mixer->rc_urb)144144+ return -ENOMEM;145145+ mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);146146+ if (!mixer->rc_setup_packet) {147147+ usb_free_urb(mixer->rc_urb);148148+ mixer->rc_urb = NULL;149149+ return -ENOMEM;150150+ }151151+ mixer->rc_setup_packet->bRequestType =152152+ USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;153153+ mixer->rc_setup_packet->bRequest = UAC_GET_MEM;154154+ mixer->rc_setup_packet->wValue = cpu_to_le16(0);155155+ mixer->rc_setup_packet->wIndex = cpu_to_le16(0);156156+ mixer->rc_setup_packet->wLength = cpu_to_le16(len);157157+ usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,158158+ usb_rcvctrlpipe(mixer->chip->dev, 0),159159+ (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,160160+ snd_usb_soundblaster_remote_complete, mixer);161161+ return 0;162162+}163163+164164+#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info165165+166166+static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)167167+{168168+ struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);169169+ int index = kcontrol->private_value;170170+171171+ ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];172172+ return 0;173173+}174174+175175+static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)176176+{177177+ struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);178178+ int index = kcontrol->private_value;179179+ int value = ucontrol->value.integer.value[0];180180+ int err, changed;181181+182182+ if (value > 1)183183+ return -EINVAL;184184+ changed = value != mixer->audigy2nx_leds[index];185185+ err = snd_usb_ctl_msg(mixer->chip->dev,186186+ usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,187187+ USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,188188+ value, index + 2, NULL, 0, 100);189189+ if (err < 0)190190+ return err;191191+ mixer->audigy2nx_leds[index] = value;192192+ return changed;193193+}194194+195195+static struct snd_kcontrol_new snd_audigy2nx_controls[] = {196196+ {197197+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,198198+ .name = "CMSS LED Switch",199199+ .info = snd_audigy2nx_led_info,200200+ .get = snd_audigy2nx_led_get,201201+ .put = snd_audigy2nx_led_put,202202+ .private_value = 0,203203+ },204204+ {205205+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,206206+ .name = "Power LED Switch",207207+ .info = snd_audigy2nx_led_info,208208+ .get = snd_audigy2nx_led_get,209209+ .put = snd_audigy2nx_led_put,210210+ .private_value = 1,211211+ },212212+ {213213+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,214214+ .name = "Dolby Digital LED Switch",215215+ .info = snd_audigy2nx_led_info,216216+ .get = snd_audigy2nx_led_get,217217+ .put = snd_audigy2nx_led_put,218218+ .private_value = 2,219219+ },220220+};221221+222222+static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)223223+{224224+ int i, err;225225+226226+ for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {227227+ if (i > 1 && /* Live24ext has 2 LEDs only */228228+ (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||229229+ mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))230230+ break; 231231+ err = snd_ctl_add(mixer->chip->card,232232+ snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));233233+ if (err < 0)234234+ return err;235235+ }236236+ mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */237237+ return 0;238238+}239239+240240+static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,241241+ struct snd_info_buffer *buffer)242242+{243243+ static const struct sb_jack {244244+ int unitid;245245+ const char *name;246246+ } jacks_audigy2nx[] = {247247+ {4, "dig in "},248248+ {7, "line in"},249249+ {19, "spk out"},250250+ {20, "hph out"},251251+ {-1, NULL}252252+ }, jacks_live24ext[] = {253253+ {4, "line in"}, /* &1=Line, &2=Mic*/254254+ {3, "hph out"}, /* headphones */255255+ {0, "RC "}, /* last command, 6 bytes see rc_config above */256256+ {-1, NULL}257257+ };258258+ const struct sb_jack *jacks;259259+ struct usb_mixer_interface *mixer = entry->private_data;260260+ int i, err;261261+ u8 buf[3];262262+263263+ snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);264264+ if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))265265+ jacks = jacks_audigy2nx;266266+ else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||267267+ mixer->chip->usb_id == USB_ID(0x041e, 0x3048))268268+ jacks = jacks_live24ext;269269+ else270270+ return;271271+272272+ for (i = 0; jacks[i].name; ++i) {273273+ snd_iprintf(buffer, "%s: ", jacks[i].name);274274+ err = snd_usb_ctl_msg(mixer->chip->dev,275275+ usb_rcvctrlpipe(mixer->chip->dev, 0),276276+ UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |277277+ USB_RECIP_INTERFACE, 0,278278+ jacks[i].unitid << 8, buf, 3, 100);279279+ if (err == 3 && (buf[0] == 3 || buf[0] == 6))280280+ snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);281281+ else282282+ snd_iprintf(buffer, "?\n");283283+ }284284+}285285+286286+static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,287287+ struct snd_ctl_elem_value *ucontrol)288288+{289289+ struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);290290+291291+ ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);292292+ return 0;293293+}294294+295295+static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,296296+ struct snd_ctl_elem_value *ucontrol)297297+{298298+ struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);299299+ u8 old_status, new_status;300300+ int err, changed;301301+302302+ old_status = mixer->xonar_u1_status;303303+ if (ucontrol->value.integer.value[0])304304+ new_status = old_status | 0x02;305305+ else306306+ new_status = old_status & ~0x02;307307+ changed = new_status != old_status;308308+ err = snd_usb_ctl_msg(mixer->chip->dev,309309+ usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,310310+ USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,311311+ 50, 0, &new_status, 1, 100);312312+ if (err < 0)313313+ return err;314314+ mixer->xonar_u1_status = new_status;315315+ return changed;316316+}317317+318318+static struct snd_kcontrol_new snd_xonar_u1_output_switch = {319319+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,320320+ .name = "Digital Playback Switch",321321+ .info = snd_ctl_boolean_mono_info,322322+ .get = snd_xonar_u1_switch_get,323323+ .put = snd_xonar_u1_switch_put,324324+};325325+326326+static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)327327+{328328+ int err;329329+330330+ err = snd_ctl_add(mixer->chip->card,331331+ snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));332332+ if (err < 0)333333+ return err;334334+ mixer->xonar_u1_status = 0x05;335335+ return 0;336336+}337337+338338+void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,339339+ unsigned char samplerate_id)340340+{341341+ struct usb_mixer_interface *mixer;342342+ struct usb_mixer_elem_info *cval;343343+ int unitid = 12; /* SamleRate ExtensionUnit ID */344344+345345+ list_for_each_entry(mixer, &chip->mixer_list, list) {346346+ cval = mixer->id_elems[unitid];347347+ if (cval) {348348+ snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,349349+ cval->control << 8,350350+ samplerate_id);351351+ snd_usb_mixer_notify_id(mixer, unitid);352352+ }353353+ break;354354+ }355355+}356356+357357+int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)358358+{359359+ int err;360360+ struct snd_info_entry *entry;361361+362362+ if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)363363+ return err;364364+365365+ if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||366366+ mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||367367+ mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {368368+ if ((err = snd_audigy2nx_controls_create(mixer)) < 0)369369+ return err;370370+ if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))371371+ snd_info_set_text_ops(entry, mixer,372372+ snd_audigy2nx_proc_read);373373+ }374374+375375+ if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||376376+ mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {377377+ err = snd_xonar_u1_controls_create(mixer);378378+ if (err < 0)379379+ return err;380380+ }381381+382382+ return 0;383383+}384384+385385+void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,386386+ int unitid)387387+{388388+ if (!mixer->rc_cfg)389389+ return;390390+ /* unit ids specific to Extigy/Audigy 2 NX: */391391+ switch (unitid) {392392+ case 0: /* remote control */393393+ mixer->rc_urb->dev = mixer->chip->dev;394394+ usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);395395+ break;396396+ case 4: /* digital in jack */397397+ case 7: /* line in jacks */398398+ case 19: /* speaker out jacks */399399+ case 20: /* headphones out jack */400400+ break;401401+ /* live24ext: 4 = line-in jack */402402+ case 3: /* hp-out jack (may actuate Mute) */403403+ if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||404404+ mixer->chip->usb_id == USB_ID(0x041e, 0x3048))405405+ snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);406406+ break;407407+ default:408408+ snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);409409+ break;410410+ }411411+}412412+
···11+/*22+ * This program is free software; you can redistribute it and/or modify33+ * it under the terms of the GNU General Public License as published by44+ * the Free Software Foundation; either version 2 of the License, or55+ * (at your option) any later version.66+ *77+ * This program is distributed in the hope that it will be useful,88+ * but WITHOUT ANY WARRANTY; without even the implied warranty of99+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1010+ * GNU General Public License for more details.1111+ *1212+ * You should have received a copy of the GNU General Public License1313+ * along with this program; if not, write to the Free Software1414+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA1515+ *1616+ */1717+1818+#include <linux/gfp.h>1919+#include <linux/init.h>2020+#include <linux/usb.h>2121+#include <linux/usb/audio.h>2222+2323+#include <sound/core.h>2424+#include <sound/pcm.h>2525+2626+#include "usbaudio.h"2727+#include "helper.h"2828+#include "card.h"2929+#include "urb.h"3030+#include "pcm.h"3131+3232+/*3333+ * convert a sampling rate into our full speed format (fs/1000 in Q16.16)3434+ * this will overflow at approx 524 kHz3535+ */3636+static inline unsigned get_usb_full_speed_rate(unsigned int rate)3737+{3838+ return ((rate << 13) + 62) / 125;3939+}4040+4141+/*4242+ * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)4343+ * this will overflow at approx 4 MHz4444+ */4545+static inline unsigned get_usb_high_speed_rate(unsigned int rate)4646+{4747+ return ((rate << 10) + 62) / 125;4848+}4949+5050+/*5151+ * unlink active urbs.5252+ */5353+static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)5454+{5555+ struct snd_usb_audio *chip = subs->stream->chip;5656+ unsigned int i;5757+ int async;5858+5959+ subs->running = 0;6060+6161+ if (!force && subs->stream->chip->shutdown) /* to be sure... */6262+ return -EBADFD;6363+6464+ async = !can_sleep && chip->async_unlink;6565+6666+ if (!async && in_interrupt())6767+ return 0;6868+6969+ for (i = 0; i < subs->nurbs; i++) {7070+ if (test_bit(i, &subs->active_mask)) {7171+ if (!test_and_set_bit(i, &subs->unlink_mask)) {7272+ struct urb *u = subs->dataurb[i].urb;7373+ if (async)7474+ usb_unlink_urb(u);7575+ else7676+ usb_kill_urb(u);7777+ }7878+ }7979+ }8080+ if (subs->syncpipe) {8181+ for (i = 0; i < SYNC_URBS; i++) {8282+ if (test_bit(i+16, &subs->active_mask)) {8383+ if (!test_and_set_bit(i+16, &subs->unlink_mask)) {8484+ struct urb *u = subs->syncurb[i].urb;8585+ if (async)8686+ usb_unlink_urb(u);8787+ else8888+ usb_kill_urb(u);8989+ }9090+ }9191+ }9292+ }9393+ return 0;9494+}9595+9696+9797+/*9898+ * release a urb data9999+ */100100+static void release_urb_ctx(struct snd_urb_ctx *u)101101+{102102+ if (u->urb) {103103+ if (u->buffer_size)104104+ usb_buffer_free(u->subs->dev, u->buffer_size,105105+ u->urb->transfer_buffer,106106+ u->urb->transfer_dma);107107+ usb_free_urb(u->urb);108108+ u->urb = NULL;109109+ }110110+}111111+112112+/*113113+ * wait until all urbs are processed.114114+ */115115+static int wait_clear_urbs(struct snd_usb_substream *subs)116116+{117117+ unsigned long end_time = jiffies + msecs_to_jiffies(1000);118118+ unsigned int i;119119+ int alive;120120+121121+ do {122122+ alive = 0;123123+ for (i = 0; i < subs->nurbs; i++) {124124+ if (test_bit(i, &subs->active_mask))125125+ alive++;126126+ }127127+ if (subs->syncpipe) {128128+ for (i = 0; i < SYNC_URBS; i++) {129129+ if (test_bit(i + 16, &subs->active_mask))130130+ alive++;131131+ }132132+ }133133+ if (! alive)134134+ break;135135+ schedule_timeout_uninterruptible(1);136136+ } while (time_before(jiffies, end_time));137137+ if (alive)138138+ snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);139139+ return 0;140140+}141141+142142+/*143143+ * release a substream144144+ */145145+void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)146146+{147147+ int i;148148+149149+ /* stop urbs (to be sure) */150150+ deactivate_urbs(subs, force, 1);151151+ wait_clear_urbs(subs);152152+153153+ for (i = 0; i < MAX_URBS; i++)154154+ release_urb_ctx(&subs->dataurb[i]);155155+ for (i = 0; i < SYNC_URBS; i++)156156+ release_urb_ctx(&subs->syncurb[i]);157157+ usb_buffer_free(subs->dev, SYNC_URBS * 4,158158+ subs->syncbuf, subs->sync_dma);159159+ subs->syncbuf = NULL;160160+ subs->nurbs = 0;161161+}162162+163163+/*164164+ * complete callback from data urb165165+ */166166+static void snd_complete_urb(struct urb *urb)167167+{168168+ struct snd_urb_ctx *ctx = urb->context;169169+ struct snd_usb_substream *subs = ctx->subs;170170+ struct snd_pcm_substream *substream = ctx->subs->pcm_substream;171171+ int err = 0;172172+173173+ if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||174174+ !subs->running || /* can be stopped during retire callback */175175+ (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||176176+ (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {177177+ clear_bit(ctx->index, &subs->active_mask);178178+ if (err < 0) {179179+ snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);180180+ snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);181181+ }182182+ }183183+}184184+185185+186186+/*187187+ * complete callback from sync urb188188+ */189189+static void snd_complete_sync_urb(struct urb *urb)190190+{191191+ struct snd_urb_ctx *ctx = urb->context;192192+ struct snd_usb_substream *subs = ctx->subs;193193+ struct snd_pcm_substream *substream = ctx->subs->pcm_substream;194194+ int err = 0;195195+196196+ if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||197197+ !subs->running || /* can be stopped during retire callback */198198+ (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||199199+ (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {200200+ clear_bit(ctx->index + 16, &subs->active_mask);201201+ if (err < 0) {202202+ snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);203203+ snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);204204+ }205205+ }206206+}207207+208208+209209+/*210210+ * initialize a substream for plaback/capture211211+ */212212+int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,213213+ unsigned int period_bytes,214214+ unsigned int rate,215215+ unsigned int frame_bits)216216+{217217+ unsigned int maxsize, i;218218+ int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;219219+ unsigned int urb_packs, total_packs, packs_per_ms;220220+ struct snd_usb_audio *chip = subs->stream->chip;221221+222222+ /* calculate the frequency in 16.16 format */223223+ if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)224224+ subs->freqn = get_usb_full_speed_rate(rate);225225+ else226226+ subs->freqn = get_usb_high_speed_rate(rate);227227+ subs->freqm = subs->freqn;228228+ /* calculate max. frequency */229229+ if (subs->maxpacksize) {230230+ /* whatever fits into a max. size packet */231231+ maxsize = subs->maxpacksize;232232+ subs->freqmax = (maxsize / (frame_bits >> 3))233233+ << (16 - subs->datainterval);234234+ } else {235235+ /* no max. packet size: just take 25% higher than nominal */236236+ subs->freqmax = subs->freqn + (subs->freqn >> 2);237237+ maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))238238+ >> (16 - subs->datainterval);239239+ }240240+ subs->phase = 0;241241+242242+ if (subs->fill_max)243243+ subs->curpacksize = subs->maxpacksize;244244+ else245245+ subs->curpacksize = maxsize;246246+247247+ if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)248248+ packs_per_ms = 8 >> subs->datainterval;249249+ else250250+ packs_per_ms = 1;251251+252252+ if (is_playback) {253253+ urb_packs = max(chip->nrpacks, 1);254254+ urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);255255+ } else256256+ urb_packs = 1;257257+ urb_packs *= packs_per_ms;258258+ if (subs->syncpipe)259259+ urb_packs = min(urb_packs, 1U << subs->syncinterval);260260+261261+ /* decide how many packets to be used */262262+ if (is_playback) {263263+ unsigned int minsize, maxpacks;264264+ /* determine how small a packet can be */265265+ minsize = (subs->freqn >> (16 - subs->datainterval))266266+ * (frame_bits >> 3);267267+ /* with sync from device, assume it can be 12% lower */268268+ if (subs->syncpipe)269269+ minsize -= minsize >> 3;270270+ minsize = max(minsize, 1u);271271+ total_packs = (period_bytes + minsize - 1) / minsize;272272+ /* we need at least two URBs for queueing */273273+ if (total_packs < 2) {274274+ total_packs = 2;275275+ } else {276276+ /* and we don't want too long a queue either */277277+ maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);278278+ total_packs = min(total_packs, maxpacks);279279+ }280280+ } else {281281+ while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)282282+ urb_packs >>= 1;283283+ total_packs = MAX_URBS * urb_packs;284284+ }285285+ subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;286286+ if (subs->nurbs > MAX_URBS) {287287+ /* too much... */288288+ subs->nurbs = MAX_URBS;289289+ total_packs = MAX_URBS * urb_packs;290290+ } else if (subs->nurbs < 2) {291291+ /* too little - we need at least two packets292292+ * to ensure contiguous playback/capture293293+ */294294+ subs->nurbs = 2;295295+ }296296+297297+ /* allocate and initialize data urbs */298298+ for (i = 0; i < subs->nurbs; i++) {299299+ struct snd_urb_ctx *u = &subs->dataurb[i];300300+ u->index = i;301301+ u->subs = subs;302302+ u->packets = (i + 1) * total_packs / subs->nurbs303303+ - i * total_packs / subs->nurbs;304304+ u->buffer_size = maxsize * u->packets;305305+ if (subs->fmt_type == UAC_FORMAT_TYPE_II)306306+ u->packets++; /* for transfer delimiter */307307+ u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);308308+ if (!u->urb)309309+ goto out_of_memory;310310+ u->urb->transfer_buffer =311311+ usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,312312+ &u->urb->transfer_dma);313313+ if (!u->urb->transfer_buffer)314314+ goto out_of_memory;315315+ u->urb->pipe = subs->datapipe;316316+ u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;317317+ u->urb->interval = 1 << subs->datainterval;318318+ u->urb->context = u;319319+ u->urb->complete = snd_complete_urb;320320+ }321321+322322+ if (subs->syncpipe) {323323+ /* allocate and initialize sync urbs */324324+ subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,325325+ GFP_KERNEL, &subs->sync_dma);326326+ if (!subs->syncbuf)327327+ goto out_of_memory;328328+ for (i = 0; i < SYNC_URBS; i++) {329329+ struct snd_urb_ctx *u = &subs->syncurb[i];330330+ u->index = i;331331+ u->subs = subs;332332+ u->packets = 1;333333+ u->urb = usb_alloc_urb(1, GFP_KERNEL);334334+ if (!u->urb)335335+ goto out_of_memory;336336+ u->urb->transfer_buffer = subs->syncbuf + i * 4;337337+ u->urb->transfer_dma = subs->sync_dma + i * 4;338338+ u->urb->transfer_buffer_length = 4;339339+ u->urb->pipe = subs->syncpipe;340340+ u->urb->transfer_flags = URB_ISO_ASAP |341341+ URB_NO_TRANSFER_DMA_MAP;342342+ u->urb->number_of_packets = 1;343343+ u->urb->interval = 1 << subs->syncinterval;344344+ u->urb->context = u;345345+ u->urb->complete = snd_complete_sync_urb;346346+ }347347+ }348348+ return 0;349349+350350+out_of_memory:351351+ snd_usb_release_substream_urbs(subs, 0);352352+ return -ENOMEM;353353+}354354+355355+/*356356+ * prepare urb for full speed capture sync pipe357357+ *358358+ * fill the length and offset of each urb descriptor.359359+ * the fixed 10.14 frequency is passed through the pipe.360360+ */361361+static int prepare_capture_sync_urb(struct snd_usb_substream *subs,362362+ struct snd_pcm_runtime *runtime,363363+ struct urb *urb)364364+{365365+ unsigned char *cp = urb->transfer_buffer;366366+ struct snd_urb_ctx *ctx = urb->context;367367+368368+ urb->dev = ctx->subs->dev; /* we need to set this at each time */369369+ urb->iso_frame_desc[0].length = 3;370370+ urb->iso_frame_desc[0].offset = 0;371371+ cp[0] = subs->freqn >> 2;372372+ cp[1] = subs->freqn >> 10;373373+ cp[2] = subs->freqn >> 18;374374+ return 0;375375+}376376+377377+/*378378+ * prepare urb for high speed capture sync pipe379379+ *380380+ * fill the length and offset of each urb descriptor.381381+ * the fixed 12.13 frequency is passed as 16.16 through the pipe.382382+ */383383+static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,384384+ struct snd_pcm_runtime *runtime,385385+ struct urb *urb)386386+{387387+ unsigned char *cp = urb->transfer_buffer;388388+ struct snd_urb_ctx *ctx = urb->context;389389+390390+ urb->dev = ctx->subs->dev; /* we need to set this at each time */391391+ urb->iso_frame_desc[0].length = 4;392392+ urb->iso_frame_desc[0].offset = 0;393393+ cp[0] = subs->freqn;394394+ cp[1] = subs->freqn >> 8;395395+ cp[2] = subs->freqn >> 16;396396+ cp[3] = subs->freqn >> 24;397397+ return 0;398398+}399399+400400+/*401401+ * process after capture sync complete402402+ * - nothing to do403403+ */404404+static int retire_capture_sync_urb(struct snd_usb_substream *subs,405405+ struct snd_pcm_runtime *runtime,406406+ struct urb *urb)407407+{408408+ return 0;409409+}410410+411411+/*412412+ * prepare urb for capture data pipe413413+ *414414+ * fill the offset and length of each descriptor.415415+ *416416+ * we use a temporary buffer to write the captured data.417417+ * since the length of written data is determined by host, we cannot418418+ * write onto the pcm buffer directly... the data is thus copied419419+ * later at complete callback to the global buffer.420420+ */421421+static int prepare_capture_urb(struct snd_usb_substream *subs,422422+ struct snd_pcm_runtime *runtime,423423+ struct urb *urb)424424+{425425+ int i, offs;426426+ struct snd_urb_ctx *ctx = urb->context;427427+428428+ offs = 0;429429+ urb->dev = ctx->subs->dev; /* we need to set this at each time */430430+ for (i = 0; i < ctx->packets; i++) {431431+ urb->iso_frame_desc[i].offset = offs;432432+ urb->iso_frame_desc[i].length = subs->curpacksize;433433+ offs += subs->curpacksize;434434+ }435435+ urb->transfer_buffer_length = offs;436436+ urb->number_of_packets = ctx->packets;437437+ return 0;438438+}439439+440440+/*441441+ * process after capture complete442442+ *443443+ * copy the data from each desctiptor to the pcm buffer, and444444+ * update the current position.445445+ */446446+static int retire_capture_urb(struct snd_usb_substream *subs,447447+ struct snd_pcm_runtime *runtime,448448+ struct urb *urb)449449+{450450+ unsigned long flags;451451+ unsigned char *cp;452452+ int i;453453+ unsigned int stride, frames, bytes, oldptr;454454+ int period_elapsed = 0;455455+456456+ stride = runtime->frame_bits >> 3;457457+458458+ for (i = 0; i < urb->number_of_packets; i++) {459459+ cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;460460+ if (urb->iso_frame_desc[i].status) {461461+ snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);462462+ // continue;463463+ }464464+ bytes = urb->iso_frame_desc[i].actual_length;465465+ frames = bytes / stride;466466+ if (!subs->txfr_quirk)467467+ bytes = frames * stride;468468+ if (bytes % (runtime->sample_bits >> 3) != 0) {469469+#ifdef CONFIG_SND_DEBUG_VERBOSE470470+ int oldbytes = bytes;471471+#endif472472+ bytes = frames * stride;473473+ snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",474474+ oldbytes, bytes);475475+ }476476+ /* update the current pointer */477477+ spin_lock_irqsave(&subs->lock, flags);478478+ oldptr = subs->hwptr_done;479479+ subs->hwptr_done += bytes;480480+ if (subs->hwptr_done >= runtime->buffer_size * stride)481481+ subs->hwptr_done -= runtime->buffer_size * stride;482482+ frames = (bytes + (oldptr % stride)) / stride;483483+ subs->transfer_done += frames;484484+ if (subs->transfer_done >= runtime->period_size) {485485+ subs->transfer_done -= runtime->period_size;486486+ period_elapsed = 1;487487+ }488488+ spin_unlock_irqrestore(&subs->lock, flags);489489+ /* copy a data chunk */490490+ if (oldptr + bytes > runtime->buffer_size * stride) {491491+ unsigned int bytes1 =492492+ runtime->buffer_size * stride - oldptr;493493+ memcpy(runtime->dma_area + oldptr, cp, bytes1);494494+ memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);495495+ } else {496496+ memcpy(runtime->dma_area + oldptr, cp, bytes);497497+ }498498+ }499499+ if (period_elapsed)500500+ snd_pcm_period_elapsed(subs->pcm_substream);501501+ return 0;502502+}503503+504504+/*505505+ * Process after capture complete when paused. Nothing to do.506506+ */507507+static int retire_paused_capture_urb(struct snd_usb_substream *subs,508508+ struct snd_pcm_runtime *runtime,509509+ struct urb *urb)510510+{511511+ return 0;512512+}513513+514514+515515+/*516516+ * prepare urb for full speed playback sync pipe517517+ *518518+ * set up the offset and length to receive the current frequency.519519+ */520520+521521+static int prepare_playback_sync_urb(struct snd_usb_substream *subs,522522+ struct snd_pcm_runtime *runtime,523523+ struct urb *urb)524524+{525525+ struct snd_urb_ctx *ctx = urb->context;526526+527527+ urb->dev = ctx->subs->dev; /* we need to set this at each time */528528+ urb->iso_frame_desc[0].length = 3;529529+ urb->iso_frame_desc[0].offset = 0;530530+ return 0;531531+}532532+533533+/*534534+ * prepare urb for high speed playback sync pipe535535+ *536536+ * set up the offset and length to receive the current frequency.537537+ */538538+539539+static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,540540+ struct snd_pcm_runtime *runtime,541541+ struct urb *urb)542542+{543543+ struct snd_urb_ctx *ctx = urb->context;544544+545545+ urb->dev = ctx->subs->dev; /* we need to set this at each time */546546+ urb->iso_frame_desc[0].length = 4;547547+ urb->iso_frame_desc[0].offset = 0;548548+ return 0;549549+}550550+551551+/*552552+ * process after full speed playback sync complete553553+ *554554+ * retrieve the current 10.14 frequency from pipe, and set it.555555+ * the value is referred in prepare_playback_urb().556556+ */557557+static int retire_playback_sync_urb(struct snd_usb_substream *subs,558558+ struct snd_pcm_runtime *runtime,559559+ struct urb *urb)560560+{561561+ unsigned int f;562562+ unsigned long flags;563563+564564+ if (urb->iso_frame_desc[0].status == 0 &&565565+ urb->iso_frame_desc[0].actual_length == 3) {566566+ f = combine_triple((u8*)urb->transfer_buffer) << 2;567567+ if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {568568+ spin_lock_irqsave(&subs->lock, flags);569569+ subs->freqm = f;570570+ spin_unlock_irqrestore(&subs->lock, flags);571571+ }572572+ }573573+574574+ return 0;575575+}576576+577577+/*578578+ * process after high speed playback sync complete579579+ *580580+ * retrieve the current 12.13 frequency from pipe, and set it.581581+ * the value is referred in prepare_playback_urb().582582+ */583583+static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,584584+ struct snd_pcm_runtime *runtime,585585+ struct urb *urb)586586+{587587+ unsigned int f;588588+ unsigned long flags;589589+590590+ if (urb->iso_frame_desc[0].status == 0 &&591591+ urb->iso_frame_desc[0].actual_length == 4) {592592+ f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;593593+ if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {594594+ spin_lock_irqsave(&subs->lock, flags);595595+ subs->freqm = f;596596+ spin_unlock_irqrestore(&subs->lock, flags);597597+ }598598+ }599599+600600+ return 0;601601+}602602+603603+/*604604+ * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete605605+ *606606+ * These devices return the number of samples per packet instead of the number607607+ * of samples per microframe.608608+ */609609+static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,610610+ struct snd_pcm_runtime *runtime,611611+ struct urb *urb)612612+{613613+ unsigned int f;614614+ unsigned long flags;615615+616616+ if (urb->iso_frame_desc[0].status == 0 &&617617+ urb->iso_frame_desc[0].actual_length == 4) {618618+ f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;619619+ f >>= subs->datainterval;620620+ if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {621621+ spin_lock_irqsave(&subs->lock, flags);622622+ subs->freqm = f;623623+ spin_unlock_irqrestore(&subs->lock, flags);624624+ }625625+ }626626+627627+ return 0;628628+}629629+630630+/* determine the number of frames in the next packet */631631+static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)632632+{633633+ if (subs->fill_max)634634+ return subs->maxframesize;635635+ else {636636+ subs->phase = (subs->phase & 0xffff)637637+ + (subs->freqm << subs->datainterval);638638+ return min(subs->phase >> 16, subs->maxframesize);639639+ }640640+}641641+642642+/*643643+ * Prepare urb for streaming before playback starts or when paused.644644+ *645645+ * We don't have any data, so we send silence.646646+ */647647+static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,648648+ struct snd_pcm_runtime *runtime,649649+ struct urb *urb)650650+{651651+ unsigned int i, offs, counts;652652+ struct snd_urb_ctx *ctx = urb->context;653653+ int stride = runtime->frame_bits >> 3;654654+655655+ offs = 0;656656+ urb->dev = ctx->subs->dev;657657+ for (i = 0; i < ctx->packets; ++i) {658658+ counts = snd_usb_audio_next_packet_size(subs);659659+ urb->iso_frame_desc[i].offset = offs * stride;660660+ urb->iso_frame_desc[i].length = counts * stride;661661+ offs += counts;662662+ }663663+ urb->number_of_packets = ctx->packets;664664+ urb->transfer_buffer_length = offs * stride;665665+ memset(urb->transfer_buffer,666666+ runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,667667+ offs * stride);668668+ return 0;669669+}670670+671671+/*672672+ * prepare urb for playback data pipe673673+ *674674+ * Since a URB can handle only a single linear buffer, we must use double675675+ * buffering when the data to be transferred overflows the buffer boundary.676676+ * To avoid inconsistencies when updating hwptr_done, we use double buffering677677+ * for all URBs.678678+ */679679+static int prepare_playback_urb(struct snd_usb_substream *subs,680680+ struct snd_pcm_runtime *runtime,681681+ struct urb *urb)682682+{683683+ int i, stride;684684+ unsigned int counts, frames, bytes;685685+ unsigned long flags;686686+ int period_elapsed = 0;687687+ struct snd_urb_ctx *ctx = urb->context;688688+689689+ stride = runtime->frame_bits >> 3;690690+691691+ frames = 0;692692+ urb->dev = ctx->subs->dev; /* we need to set this at each time */693693+ urb->number_of_packets = 0;694694+ spin_lock_irqsave(&subs->lock, flags);695695+ for (i = 0; i < ctx->packets; i++) {696696+ counts = snd_usb_audio_next_packet_size(subs);697697+ /* set up descriptor */698698+ urb->iso_frame_desc[i].offset = frames * stride;699699+ urb->iso_frame_desc[i].length = counts * stride;700700+ frames += counts;701701+ urb->number_of_packets++;702702+ subs->transfer_done += counts;703703+ if (subs->transfer_done >= runtime->period_size) {704704+ subs->transfer_done -= runtime->period_size;705705+ period_elapsed = 1;706706+ if (subs->fmt_type == UAC_FORMAT_TYPE_II) {707707+ if (subs->transfer_done > 0) {708708+ /* FIXME: fill-max mode is not709709+ * supported yet */710710+ frames -= subs->transfer_done;711711+ counts -= subs->transfer_done;712712+ urb->iso_frame_desc[i].length =713713+ counts * stride;714714+ subs->transfer_done = 0;715715+ }716716+ i++;717717+ if (i < ctx->packets) {718718+ /* add a transfer delimiter */719719+ urb->iso_frame_desc[i].offset =720720+ frames * stride;721721+ urb->iso_frame_desc[i].length = 0;722722+ urb->number_of_packets++;723723+ }724724+ break;725725+ }726726+ }727727+ if (period_elapsed) /* finish at the period boundary */728728+ break;729729+ }730730+ bytes = frames * stride;731731+ if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {732732+ /* err, the transferred area goes over buffer boundary. */733733+ unsigned int bytes1 =734734+ runtime->buffer_size * stride - subs->hwptr_done;735735+ memcpy(urb->transfer_buffer,736736+ runtime->dma_area + subs->hwptr_done, bytes1);737737+ memcpy(urb->transfer_buffer + bytes1,738738+ runtime->dma_area, bytes - bytes1);739739+ } else {740740+ memcpy(urb->transfer_buffer,741741+ runtime->dma_area + subs->hwptr_done, bytes);742742+ }743743+ subs->hwptr_done += bytes;744744+ if (subs->hwptr_done >= runtime->buffer_size * stride)745745+ subs->hwptr_done -= runtime->buffer_size * stride;746746+ runtime->delay += frames;747747+ spin_unlock_irqrestore(&subs->lock, flags);748748+ urb->transfer_buffer_length = bytes;749749+ if (period_elapsed)750750+ snd_pcm_period_elapsed(subs->pcm_substream);751751+ return 0;752752+}753753+754754+/*755755+ * process after playback data complete756756+ * - decrease the delay count again757757+ */758758+static int retire_playback_urb(struct snd_usb_substream *subs,759759+ struct snd_pcm_runtime *runtime,760760+ struct urb *urb)761761+{762762+ unsigned long flags;763763+ int stride = runtime->frame_bits >> 3;764764+ int processed = urb->transfer_buffer_length / stride;765765+766766+ spin_lock_irqsave(&subs->lock, flags);767767+ if (processed > runtime->delay)768768+ runtime->delay = 0;769769+ else770770+ runtime->delay -= processed;771771+ spin_unlock_irqrestore(&subs->lock, flags);772772+ return 0;773773+}774774+775775+static const char *usb_error_string(int err)776776+{777777+ switch (err) {778778+ case -ENODEV:779779+ return "no device";780780+ case -ENOENT:781781+ return "endpoint not enabled";782782+ case -EPIPE:783783+ return "endpoint stalled";784784+ case -ENOSPC:785785+ return "not enough bandwidth";786786+ case -ESHUTDOWN:787787+ return "device disabled";788788+ case -EHOSTUNREACH:789789+ return "device suspended";790790+ case -EINVAL:791791+ case -EAGAIN:792792+ case -EFBIG:793793+ case -EMSGSIZE:794794+ return "internal error";795795+ default:796796+ return "unknown error";797797+ }798798+}799799+800800+/*801801+ * set up and start data/sync urbs802802+ */803803+static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)804804+{805805+ unsigned int i;806806+ int err;807807+808808+ if (subs->stream->chip->shutdown)809809+ return -EBADFD;810810+811811+ for (i = 0; i < subs->nurbs; i++) {812812+ if (snd_BUG_ON(!subs->dataurb[i].urb))813813+ return -EINVAL;814814+ if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {815815+ snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);816816+ goto __error;817817+ }818818+ }819819+ if (subs->syncpipe) {820820+ for (i = 0; i < SYNC_URBS; i++) {821821+ if (snd_BUG_ON(!subs->syncurb[i].urb))822822+ return -EINVAL;823823+ if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {824824+ snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);825825+ goto __error;826826+ }827827+ }828828+ }829829+830830+ subs->active_mask = 0;831831+ subs->unlink_mask = 0;832832+ subs->running = 1;833833+ for (i = 0; i < subs->nurbs; i++) {834834+ err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);835835+ if (err < 0) {836836+ snd_printk(KERN_ERR "cannot submit datapipe "837837+ "for urb %d, error %d: %s\n",838838+ i, err, usb_error_string(err));839839+ goto __error;840840+ }841841+ set_bit(i, &subs->active_mask);842842+ }843843+ if (subs->syncpipe) {844844+ for (i = 0; i < SYNC_URBS; i++) {845845+ err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);846846+ if (err < 0) {847847+ snd_printk(KERN_ERR "cannot submit syncpipe "848848+ "for urb %d, error %d: %s\n",849849+ i, err, usb_error_string(err));850850+ goto __error;851851+ }852852+ set_bit(i + 16, &subs->active_mask);853853+ }854854+ }855855+ return 0;856856+857857+ __error:858858+ // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);859859+ deactivate_urbs(subs, 0, 0);860860+ return -EPIPE;861861+}862862+863863+864864+/*865865+ */866866+static struct snd_urb_ops audio_urb_ops[2] = {867867+ {868868+ .prepare = prepare_nodata_playback_urb,869869+ .retire = retire_playback_urb,870870+ .prepare_sync = prepare_playback_sync_urb,871871+ .retire_sync = retire_playback_sync_urb,872872+ },873873+ {874874+ .prepare = prepare_capture_urb,875875+ .retire = retire_capture_urb,876876+ .prepare_sync = prepare_capture_sync_urb,877877+ .retire_sync = retire_capture_sync_urb,878878+ },879879+};880880+881881+static struct snd_urb_ops audio_urb_ops_high_speed[2] = {882882+ {883883+ .prepare = prepare_nodata_playback_urb,884884+ .retire = retire_playback_urb,885885+ .prepare_sync = prepare_playback_sync_urb_hs,886886+ .retire_sync = retire_playback_sync_urb_hs,887887+ },888888+ {889889+ .prepare = prepare_capture_urb,890890+ .retire = retire_capture_urb,891891+ .prepare_sync = prepare_capture_sync_urb_hs,892892+ .retire_sync = retire_capture_sync_urb,893893+ },894894+};895895+896896+/*897897+ * initialize the substream instance.898898+ */899899+900900+void snd_usb_init_substream(struct snd_usb_stream *as,901901+ int stream, struct audioformat *fp)902902+{903903+ struct snd_usb_substream *subs = &as->substream[stream];904904+905905+ INIT_LIST_HEAD(&subs->fmt_list);906906+ spin_lock_init(&subs->lock);907907+908908+ subs->stream = as;909909+ subs->direction = stream;910910+ subs->dev = as->chip->dev;911911+ subs->txfr_quirk = as->chip->txfr_quirk;912912+ if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {913913+ subs->ops = audio_urb_ops[stream];914914+ } else {915915+ subs->ops = audio_urb_ops_high_speed[stream];916916+ switch (as->chip->usb_id) {917917+ case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */918918+ case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */919919+ case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */920920+ subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;921921+ break;922922+ case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra 8 */923923+ case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */924924+ subs->ops.prepare_sync = prepare_playback_sync_urb;925925+ subs->ops.retire_sync = retire_playback_sync_urb;926926+ break;927927+ }928928+ }929929+930930+ snd_usb_set_pcm_ops(as->pcm, stream);931931+932932+ list_add_tail(&fp->list, &subs->fmt_list);933933+ subs->formats |= fp->formats;934934+ subs->endpoint = fp->endpoint;935935+ subs->num_formats++;936936+ subs->fmt_type = fp->fmt_type;937937+}938938+939939+int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)940940+{941941+ struct snd_usb_substream *subs = substream->runtime->private_data;942942+943943+ switch (cmd) {944944+ case SNDRV_PCM_TRIGGER_START:945945+ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:946946+ subs->ops.prepare = prepare_playback_urb;947947+ return 0;948948+ case SNDRV_PCM_TRIGGER_STOP:949949+ return deactivate_urbs(subs, 0, 0);950950+ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:951951+ subs->ops.prepare = prepare_nodata_playback_urb;952952+ return 0;953953+ }954954+955955+ return -EINVAL;956956+}957957+958958+int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)959959+{960960+ struct snd_usb_substream *subs = substream->runtime->private_data;961961+962962+ switch (cmd) {963963+ case SNDRV_PCM_TRIGGER_START:964964+ subs->ops.retire = retire_capture_urb;965965+ return start_urbs(subs, substream->runtime);966966+ case SNDRV_PCM_TRIGGER_STOP:967967+ return deactivate_urbs(subs, 0, 0);968968+ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:969969+ subs->ops.retire = retire_paused_capture_urb;970970+ return 0;971971+ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:972972+ subs->ops.retire = retire_capture_urb;973973+ return 0;974974+ }975975+976976+ return -EINVAL;977977+}978978+979979+int snd_usb_substream_prepare(struct snd_usb_substream *subs,980980+ struct snd_pcm_runtime *runtime)981981+{982982+ /* clear urbs (to be sure) */983983+ deactivate_urbs(subs, 0, 1);984984+ wait_clear_urbs(subs);985985+986986+ /* for playback, submit the URBs now; otherwise, the first hwptr_done987987+ * updates for all URBs would happen at the same time when starting */988988+ if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {989989+ subs->ops.prepare = prepare_nodata_playback_urb;990990+ return start_urbs(subs, runtime);991991+ }992992+993993+ return 0;994994+}995995+
+21
sound/usb/urb.h
···11+#ifndef __USBAUDIO_URB_H22+#define __USBAUDIO_URB_H33+44+void snd_usb_init_substream(struct snd_usb_stream *as,55+ int stream,66+ struct audioformat *fp);77+88+int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,99+ unsigned int period_bytes,1010+ unsigned int rate,1111+ unsigned int frame_bits);1212+1313+void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force);1414+1515+int snd_usb_substream_prepare(struct snd_usb_substream *subs,1616+ struct snd_pcm_runtime *runtime);1717+1818+int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd);1919+int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd);2020+2121+#endif /* __USBAUDIO_URB_H */
-4050
sound/usb/usbaudio.c
···11-/*22- * (Tentative) USB Audio Driver for ALSA33- *44- * Main and PCM part55- *66- * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>77- *88- * Many codes borrowed from audio.c by99- * Alan Cox (alan@lxorguk.ukuu.org.uk)1010- * Thomas Sailer (sailer@ife.ee.ethz.ch)1111- *1212- *1313- * This program is free software; you can redistribute it and/or modify1414- * it under the terms of the GNU General Public License as published by1515- * the Free Software Foundation; either version 2 of the License, or1616- * (at your option) any later version.1717- *1818- * This program is distributed in the hope that it will be useful,1919- * but WITHOUT ANY WARRANTY; without even the implied warranty of2020- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the2121- * GNU General Public License for more details.2222- *2323- * You should have received a copy of the GNU General Public License2424- * along with this program; if not, write to the Free Software2525- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA2626- *2727- *2828- * NOTES:2929- *3030- * - async unlink should be used for avoiding the sleep inside lock.3131- * 2.4.22 usb-uhci seems buggy for async unlinking and results in3232- * oops. in such a cse, pass async_unlink=0 option.3333- * - the linked URBs would be preferred but not used so far because of3434- * the instability of unlinking.3535- * - type II is not supported properly. there is no device which supports3636- * this type *correctly*. SB extigy looks as if it supports, but it's3737- * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).3838- */3939-4040-4141-#include <linux/bitops.h>4242-#include <linux/init.h>4343-#include <linux/list.h>4444-#include <linux/slab.h>4545-#include <linux/string.h>4646-#include <linux/usb.h>4747-#include <linux/moduleparam.h>4848-#include <linux/mutex.h>4949-#include <linux/usb/audio.h>5050-#include <linux/usb/ch9.h>5151-5252-#include <sound/core.h>5353-#include <sound/info.h>5454-#include <sound/pcm.h>5555-#include <sound/pcm_params.h>5656-#include <sound/initval.h>5757-5858-#include "usbaudio.h"5959-6060-6161-MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");6262-MODULE_DESCRIPTION("USB Audio");6363-MODULE_LICENSE("GPL");6464-MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");6565-6666-6767-static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */6868-static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */6969-static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */7070-/* Vendor/product IDs for this card */7171-static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };7272-static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };7373-static int nrpacks = 8; /* max. number of packets per urb */7474-static int async_unlink = 1;7575-static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/7676-static int ignore_ctl_error;7777-7878-module_param_array(index, int, NULL, 0444);7979-MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");8080-module_param_array(id, charp, NULL, 0444);8181-MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");8282-module_param_array(enable, bool, NULL, 0444);8383-MODULE_PARM_DESC(enable, "Enable USB audio adapter.");8484-module_param_array(vid, int, NULL, 0444);8585-MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");8686-module_param_array(pid, int, NULL, 0444);8787-MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");8888-module_param(nrpacks, int, 0644);8989-MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");9090-module_param(async_unlink, bool, 0444);9191-MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");9292-module_param_array(device_setup, int, NULL, 0444);9393-MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");9494-module_param(ignore_ctl_error, bool, 0444);9595-MODULE_PARM_DESC(ignore_ctl_error,9696- "Ignore errors from USB controller for mixer interfaces.");9797-9898-/*9999- * debug the h/w constraints100100- */101101-/* #define HW_CONST_DEBUG */102102-103103-104104-/*105105- *106106- */107107-108108-#define MAX_PACKS 20109109-#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */110110-#define MAX_URBS 8111111-#define SYNC_URBS 4 /* always four urbs for sync */112112-#define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */113113-114114-struct audioformat {115115- struct list_head list;116116- snd_pcm_format_t format; /* format type */117117- unsigned int channels; /* # channels */118118- unsigned int fmt_type; /* USB audio format type (1-3) */119119- unsigned int frame_size; /* samples per frame for non-audio */120120- int iface; /* interface number */121121- unsigned char altsetting; /* corresponding alternate setting */122122- unsigned char altset_idx; /* array index of altenate setting */123123- unsigned char attributes; /* corresponding attributes of cs endpoint */124124- unsigned char endpoint; /* endpoint */125125- unsigned char ep_attr; /* endpoint attributes */126126- unsigned char datainterval; /* log_2 of data packet interval */127127- unsigned int maxpacksize; /* max. packet size */128128- unsigned int rates; /* rate bitmasks */129129- unsigned int rate_min, rate_max; /* min/max rates */130130- unsigned int nr_rates; /* number of rate table entries */131131- unsigned int *rate_table; /* rate table */132132-};133133-134134-struct snd_usb_substream;135135-136136-struct snd_urb_ctx {137137- struct urb *urb;138138- unsigned int buffer_size; /* size of data buffer, if data URB */139139- struct snd_usb_substream *subs;140140- int index; /* index for urb array */141141- int packets; /* number of packets per urb */142142-};143143-144144-struct snd_urb_ops {145145- int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);146146- int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);147147- int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);148148- int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);149149-};150150-151151-struct snd_usb_substream {152152- struct snd_usb_stream *stream;153153- struct usb_device *dev;154154- struct snd_pcm_substream *pcm_substream;155155- int direction; /* playback or capture */156156- int interface; /* current interface */157157- int endpoint; /* assigned endpoint */158158- struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */159159- unsigned int cur_rate; /* current rate (for hw_params callback) */160160- unsigned int period_bytes; /* current period bytes (for hw_params callback) */161161- unsigned int format; /* USB data format */162162- unsigned int datapipe; /* the data i/o pipe */163163- unsigned int syncpipe; /* 1 - async out or adaptive in */164164- unsigned int datainterval; /* log_2 of data packet interval */165165- unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */166166- unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */167167- unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */168168- unsigned int freqmax; /* maximum sampling rate, used for buffer management */169169- unsigned int phase; /* phase accumulator */170170- unsigned int maxpacksize; /* max packet size in bytes */171171- unsigned int maxframesize; /* max packet size in frames */172172- unsigned int curpacksize; /* current packet size in bytes (for capture) */173173- unsigned int curframesize; /* current packet size in frames (for capture) */174174- unsigned int fill_max: 1; /* fill max packet size always */175175- unsigned int txfr_quirk:1; /* allow sub-frame alignment */176176- unsigned int fmt_type; /* USB audio format type (1-3) */177177-178178- unsigned int running: 1; /* running status */179179-180180- unsigned int hwptr_done; /* processed byte position in the buffer */181181- unsigned int transfer_done; /* processed frames since last period update */182182- unsigned long active_mask; /* bitmask of active urbs */183183- unsigned long unlink_mask; /* bitmask of unlinked urbs */184184-185185- unsigned int nurbs; /* # urbs */186186- struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */187187- struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */188188- char *syncbuf; /* sync buffer for all sync URBs */189189- dma_addr_t sync_dma; /* DMA address of syncbuf */190190-191191- u64 formats; /* format bitmasks (all or'ed) */192192- unsigned int num_formats; /* number of supported audio formats (list) */193193- struct list_head fmt_list; /* format list */194194- struct snd_pcm_hw_constraint_list rate_list; /* limited rates */195195- spinlock_t lock;196196-197197- struct snd_urb_ops ops; /* callbacks (must be filled at init) */198198-};199199-200200-201201-struct snd_usb_stream {202202- struct snd_usb_audio *chip;203203- struct snd_pcm *pcm;204204- int pcm_index;205205- unsigned int fmt_type; /* USB audio format type (1-3) */206206- struct snd_usb_substream substream[2];207207- struct list_head list;208208-};209209-210210-211211-/*212212- * we keep the snd_usb_audio_t instances by ourselves for merging213213- * the all interfaces on the same card as one sound device.214214- */215215-216216-static DEFINE_MUTEX(register_mutex);217217-static struct snd_usb_audio *usb_chip[SNDRV_CARDS];218218-219219-220220-/*221221- * convert a sampling rate into our full speed format (fs/1000 in Q16.16)222222- * this will overflow at approx 524 kHz223223- */224224-static inline unsigned get_usb_full_speed_rate(unsigned int rate)225225-{226226- return ((rate << 13) + 62) / 125;227227-}228228-229229-/*230230- * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)231231- * this will overflow at approx 4 MHz232232- */233233-static inline unsigned get_usb_high_speed_rate(unsigned int rate)234234-{235235- return ((rate << 10) + 62) / 125;236236-}237237-238238-/* convert our full speed USB rate into sampling rate in Hz */239239-static inline unsigned get_full_speed_hz(unsigned int usb_rate)240240-{241241- return (usb_rate * 125 + (1 << 12)) >> 13;242242-}243243-244244-/* convert our high speed USB rate into sampling rate in Hz */245245-static inline unsigned get_high_speed_hz(unsigned int usb_rate)246246-{247247- return (usb_rate * 125 + (1 << 9)) >> 10;248248-}249249-250250-251251-/*252252- * prepare urb for full speed capture sync pipe253253- *254254- * fill the length and offset of each urb descriptor.255255- * the fixed 10.14 frequency is passed through the pipe.256256- */257257-static int prepare_capture_sync_urb(struct snd_usb_substream *subs,258258- struct snd_pcm_runtime *runtime,259259- struct urb *urb)260260-{261261- unsigned char *cp = urb->transfer_buffer;262262- struct snd_urb_ctx *ctx = urb->context;263263-264264- urb->dev = ctx->subs->dev; /* we need to set this at each time */265265- urb->iso_frame_desc[0].length = 3;266266- urb->iso_frame_desc[0].offset = 0;267267- cp[0] = subs->freqn >> 2;268268- cp[1] = subs->freqn >> 10;269269- cp[2] = subs->freqn >> 18;270270- return 0;271271-}272272-273273-/*274274- * prepare urb for high speed capture sync pipe275275- *276276- * fill the length and offset of each urb descriptor.277277- * the fixed 12.13 frequency is passed as 16.16 through the pipe.278278- */279279-static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,280280- struct snd_pcm_runtime *runtime,281281- struct urb *urb)282282-{283283- unsigned char *cp = urb->transfer_buffer;284284- struct snd_urb_ctx *ctx = urb->context;285285-286286- urb->dev = ctx->subs->dev; /* we need to set this at each time */287287- urb->iso_frame_desc[0].length = 4;288288- urb->iso_frame_desc[0].offset = 0;289289- cp[0] = subs->freqn;290290- cp[1] = subs->freqn >> 8;291291- cp[2] = subs->freqn >> 16;292292- cp[3] = subs->freqn >> 24;293293- return 0;294294-}295295-296296-/*297297- * process after capture sync complete298298- * - nothing to do299299- */300300-static int retire_capture_sync_urb(struct snd_usb_substream *subs,301301- struct snd_pcm_runtime *runtime,302302- struct urb *urb)303303-{304304- return 0;305305-}306306-307307-/*308308- * prepare urb for capture data pipe309309- *310310- * fill the offset and length of each descriptor.311311- *312312- * we use a temporary buffer to write the captured data.313313- * since the length of written data is determined by host, we cannot314314- * write onto the pcm buffer directly... the data is thus copied315315- * later at complete callback to the global buffer.316316- */317317-static int prepare_capture_urb(struct snd_usb_substream *subs,318318- struct snd_pcm_runtime *runtime,319319- struct urb *urb)320320-{321321- int i, offs;322322- struct snd_urb_ctx *ctx = urb->context;323323-324324- offs = 0;325325- urb->dev = ctx->subs->dev; /* we need to set this at each time */326326- for (i = 0; i < ctx->packets; i++) {327327- urb->iso_frame_desc[i].offset = offs;328328- urb->iso_frame_desc[i].length = subs->curpacksize;329329- offs += subs->curpacksize;330330- }331331- urb->transfer_buffer_length = offs;332332- urb->number_of_packets = ctx->packets;333333- return 0;334334-}335335-336336-/*337337- * process after capture complete338338- *339339- * copy the data from each desctiptor to the pcm buffer, and340340- * update the current position.341341- */342342-static int retire_capture_urb(struct snd_usb_substream *subs,343343- struct snd_pcm_runtime *runtime,344344- struct urb *urb)345345-{346346- unsigned long flags;347347- unsigned char *cp;348348- int i;349349- unsigned int stride, frames, bytes, oldptr;350350- int period_elapsed = 0;351351-352352- stride = runtime->frame_bits >> 3;353353-354354- for (i = 0; i < urb->number_of_packets; i++) {355355- cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;356356- if (urb->iso_frame_desc[i].status) {357357- snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);358358- // continue;359359- }360360- bytes = urb->iso_frame_desc[i].actual_length;361361- frames = bytes / stride;362362- if (!subs->txfr_quirk)363363- bytes = frames * stride;364364- if (bytes % (runtime->sample_bits >> 3) != 0) {365365-#ifdef CONFIG_SND_DEBUG_VERBOSE366366- int oldbytes = bytes;367367-#endif368368- bytes = frames * stride;369369- snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",370370- oldbytes, bytes);371371- }372372- /* update the current pointer */373373- spin_lock_irqsave(&subs->lock, flags);374374- oldptr = subs->hwptr_done;375375- subs->hwptr_done += bytes;376376- if (subs->hwptr_done >= runtime->buffer_size * stride)377377- subs->hwptr_done -= runtime->buffer_size * stride;378378- frames = (bytes + (oldptr % stride)) / stride;379379- subs->transfer_done += frames;380380- if (subs->transfer_done >= runtime->period_size) {381381- subs->transfer_done -= runtime->period_size;382382- period_elapsed = 1;383383- }384384- spin_unlock_irqrestore(&subs->lock, flags);385385- /* copy a data chunk */386386- if (oldptr + bytes > runtime->buffer_size * stride) {387387- unsigned int bytes1 =388388- runtime->buffer_size * stride - oldptr;389389- memcpy(runtime->dma_area + oldptr, cp, bytes1);390390- memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);391391- } else {392392- memcpy(runtime->dma_area + oldptr, cp, bytes);393393- }394394- }395395- if (period_elapsed)396396- snd_pcm_period_elapsed(subs->pcm_substream);397397- return 0;398398-}399399-400400-/*401401- * Process after capture complete when paused. Nothing to do.402402- */403403-static int retire_paused_capture_urb(struct snd_usb_substream *subs,404404- struct snd_pcm_runtime *runtime,405405- struct urb *urb)406406-{407407- return 0;408408-}409409-410410-411411-/*412412- * prepare urb for full speed playback sync pipe413413- *414414- * set up the offset and length to receive the current frequency.415415- */416416-417417-static int prepare_playback_sync_urb(struct snd_usb_substream *subs,418418- struct snd_pcm_runtime *runtime,419419- struct urb *urb)420420-{421421- struct snd_urb_ctx *ctx = urb->context;422422-423423- urb->dev = ctx->subs->dev; /* we need to set this at each time */424424- urb->iso_frame_desc[0].length = 3;425425- urb->iso_frame_desc[0].offset = 0;426426- return 0;427427-}428428-429429-/*430430- * prepare urb for high speed playback sync pipe431431- *432432- * set up the offset and length to receive the current frequency.433433- */434434-435435-static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,436436- struct snd_pcm_runtime *runtime,437437- struct urb *urb)438438-{439439- struct snd_urb_ctx *ctx = urb->context;440440-441441- urb->dev = ctx->subs->dev; /* we need to set this at each time */442442- urb->iso_frame_desc[0].length = 4;443443- urb->iso_frame_desc[0].offset = 0;444444- return 0;445445-}446446-447447-/*448448- * process after full speed playback sync complete449449- *450450- * retrieve the current 10.14 frequency from pipe, and set it.451451- * the value is referred in prepare_playback_urb().452452- */453453-static int retire_playback_sync_urb(struct snd_usb_substream *subs,454454- struct snd_pcm_runtime *runtime,455455- struct urb *urb)456456-{457457- unsigned int f;458458- unsigned long flags;459459-460460- if (urb->iso_frame_desc[0].status == 0 &&461461- urb->iso_frame_desc[0].actual_length == 3) {462462- f = combine_triple((u8*)urb->transfer_buffer) << 2;463463- if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {464464- spin_lock_irqsave(&subs->lock, flags);465465- subs->freqm = f;466466- spin_unlock_irqrestore(&subs->lock, flags);467467- }468468- }469469-470470- return 0;471471-}472472-473473-/*474474- * process after high speed playback sync complete475475- *476476- * retrieve the current 12.13 frequency from pipe, and set it.477477- * the value is referred in prepare_playback_urb().478478- */479479-static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,480480- struct snd_pcm_runtime *runtime,481481- struct urb *urb)482482-{483483- unsigned int f;484484- unsigned long flags;485485-486486- if (urb->iso_frame_desc[0].status == 0 &&487487- urb->iso_frame_desc[0].actual_length == 4) {488488- f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;489489- if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {490490- spin_lock_irqsave(&subs->lock, flags);491491- subs->freqm = f;492492- spin_unlock_irqrestore(&subs->lock, flags);493493- }494494- }495495-496496- return 0;497497-}498498-499499-/*500500- * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete501501- *502502- * These devices return the number of samples per packet instead of the number503503- * of samples per microframe.504504- */505505-static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,506506- struct snd_pcm_runtime *runtime,507507- struct urb *urb)508508-{509509- unsigned int f;510510- unsigned long flags;511511-512512- if (urb->iso_frame_desc[0].status == 0 &&513513- urb->iso_frame_desc[0].actual_length == 4) {514514- f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;515515- f >>= subs->datainterval;516516- if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {517517- spin_lock_irqsave(&subs->lock, flags);518518- subs->freqm = f;519519- spin_unlock_irqrestore(&subs->lock, flags);520520- }521521- }522522-523523- return 0;524524-}525525-526526-/* determine the number of frames in the next packet */527527-static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)528528-{529529- if (subs->fill_max)530530- return subs->maxframesize;531531- else {532532- subs->phase = (subs->phase & 0xffff)533533- + (subs->freqm << subs->datainterval);534534- return min(subs->phase >> 16, subs->maxframesize);535535- }536536-}537537-538538-/*539539- * Prepare urb for streaming before playback starts or when paused.540540- *541541- * We don't have any data, so we send silence.542542- */543543-static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,544544- struct snd_pcm_runtime *runtime,545545- struct urb *urb)546546-{547547- unsigned int i, offs, counts;548548- struct snd_urb_ctx *ctx = urb->context;549549- int stride = runtime->frame_bits >> 3;550550-551551- offs = 0;552552- urb->dev = ctx->subs->dev;553553- for (i = 0; i < ctx->packets; ++i) {554554- counts = snd_usb_audio_next_packet_size(subs);555555- urb->iso_frame_desc[i].offset = offs * stride;556556- urb->iso_frame_desc[i].length = counts * stride;557557- offs += counts;558558- }559559- urb->number_of_packets = ctx->packets;560560- urb->transfer_buffer_length = offs * stride;561561- memset(urb->transfer_buffer,562562- subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,563563- offs * stride);564564- return 0;565565-}566566-567567-/*568568- * prepare urb for playback data pipe569569- *570570- * Since a URB can handle only a single linear buffer, we must use double571571- * buffering when the data to be transferred overflows the buffer boundary.572572- * To avoid inconsistencies when updating hwptr_done, we use double buffering573573- * for all URBs.574574- */575575-static int prepare_playback_urb(struct snd_usb_substream *subs,576576- struct snd_pcm_runtime *runtime,577577- struct urb *urb)578578-{579579- int i, stride;580580- unsigned int counts, frames, bytes;581581- unsigned long flags;582582- int period_elapsed = 0;583583- struct snd_urb_ctx *ctx = urb->context;584584-585585- stride = runtime->frame_bits >> 3;586586-587587- frames = 0;588588- urb->dev = ctx->subs->dev; /* we need to set this at each time */589589- urb->number_of_packets = 0;590590- spin_lock_irqsave(&subs->lock, flags);591591- for (i = 0; i < ctx->packets; i++) {592592- counts = snd_usb_audio_next_packet_size(subs);593593- /* set up descriptor */594594- urb->iso_frame_desc[i].offset = frames * stride;595595- urb->iso_frame_desc[i].length = counts * stride;596596- frames += counts;597597- urb->number_of_packets++;598598- subs->transfer_done += counts;599599- if (subs->transfer_done >= runtime->period_size) {600600- subs->transfer_done -= runtime->period_size;601601- period_elapsed = 1;602602- if (subs->fmt_type == UAC_FORMAT_TYPE_II) {603603- if (subs->transfer_done > 0) {604604- /* FIXME: fill-max mode is not605605- * supported yet */606606- frames -= subs->transfer_done;607607- counts -= subs->transfer_done;608608- urb->iso_frame_desc[i].length =609609- counts * stride;610610- subs->transfer_done = 0;611611- }612612- i++;613613- if (i < ctx->packets) {614614- /* add a transfer delimiter */615615- urb->iso_frame_desc[i].offset =616616- frames * stride;617617- urb->iso_frame_desc[i].length = 0;618618- urb->number_of_packets++;619619- }620620- break;621621- }622622- }623623- if (period_elapsed) /* finish at the period boundary */624624- break;625625- }626626- bytes = frames * stride;627627- if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {628628- /* err, the transferred area goes over buffer boundary. */629629- unsigned int bytes1 =630630- runtime->buffer_size * stride - subs->hwptr_done;631631- memcpy(urb->transfer_buffer,632632- runtime->dma_area + subs->hwptr_done, bytes1);633633- memcpy(urb->transfer_buffer + bytes1,634634- runtime->dma_area, bytes - bytes1);635635- } else {636636- memcpy(urb->transfer_buffer,637637- runtime->dma_area + subs->hwptr_done, bytes);638638- }639639- subs->hwptr_done += bytes;640640- if (subs->hwptr_done >= runtime->buffer_size * stride)641641- subs->hwptr_done -= runtime->buffer_size * stride;642642- runtime->delay += frames;643643- spin_unlock_irqrestore(&subs->lock, flags);644644- urb->transfer_buffer_length = bytes;645645- if (period_elapsed)646646- snd_pcm_period_elapsed(subs->pcm_substream);647647- return 0;648648-}649649-650650-/*651651- * process after playback data complete652652- * - decrease the delay count again653653- */654654-static int retire_playback_urb(struct snd_usb_substream *subs,655655- struct snd_pcm_runtime *runtime,656656- struct urb *urb)657657-{658658- unsigned long flags;659659- int stride = runtime->frame_bits >> 3;660660- int processed = urb->transfer_buffer_length / stride;661661-662662- spin_lock_irqsave(&subs->lock, flags);663663- if (processed > runtime->delay)664664- runtime->delay = 0;665665- else666666- runtime->delay -= processed;667667- spin_unlock_irqrestore(&subs->lock, flags);668668- return 0;669669-}670670-671671-672672-/*673673- */674674-static struct snd_urb_ops audio_urb_ops[2] = {675675- {676676- .prepare = prepare_nodata_playback_urb,677677- .retire = retire_playback_urb,678678- .prepare_sync = prepare_playback_sync_urb,679679- .retire_sync = retire_playback_sync_urb,680680- },681681- {682682- .prepare = prepare_capture_urb,683683- .retire = retire_capture_urb,684684- .prepare_sync = prepare_capture_sync_urb,685685- .retire_sync = retire_capture_sync_urb,686686- },687687-};688688-689689-static struct snd_urb_ops audio_urb_ops_high_speed[2] = {690690- {691691- .prepare = prepare_nodata_playback_urb,692692- .retire = retire_playback_urb,693693- .prepare_sync = prepare_playback_sync_urb_hs,694694- .retire_sync = retire_playback_sync_urb_hs,695695- },696696- {697697- .prepare = prepare_capture_urb,698698- .retire = retire_capture_urb,699699- .prepare_sync = prepare_capture_sync_urb_hs,700700- .retire_sync = retire_capture_sync_urb,701701- },702702-};703703-704704-/*705705- * complete callback from data urb706706- */707707-static void snd_complete_urb(struct urb *urb)708708-{709709- struct snd_urb_ctx *ctx = urb->context;710710- struct snd_usb_substream *subs = ctx->subs;711711- struct snd_pcm_substream *substream = ctx->subs->pcm_substream;712712- int err = 0;713713-714714- if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||715715- !subs->running || /* can be stopped during retire callback */716716- (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||717717- (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {718718- clear_bit(ctx->index, &subs->active_mask);719719- if (err < 0) {720720- snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);721721- snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);722722- }723723- }724724-}725725-726726-727727-/*728728- * complete callback from sync urb729729- */730730-static void snd_complete_sync_urb(struct urb *urb)731731-{732732- struct snd_urb_ctx *ctx = urb->context;733733- struct snd_usb_substream *subs = ctx->subs;734734- struct snd_pcm_substream *substream = ctx->subs->pcm_substream;735735- int err = 0;736736-737737- if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||738738- !subs->running || /* can be stopped during retire callback */739739- (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||740740- (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {741741- clear_bit(ctx->index + 16, &subs->active_mask);742742- if (err < 0) {743743- snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);744744- snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);745745- }746746- }747747-}748748-749749-750750-/*751751- * unlink active urbs.752752- */753753-static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)754754-{755755- unsigned int i;756756- int async;757757-758758- subs->running = 0;759759-760760- if (!force && subs->stream->chip->shutdown) /* to be sure... */761761- return -EBADFD;762762-763763- async = !can_sleep && async_unlink;764764-765765- if (!async && in_interrupt())766766- return 0;767767-768768- for (i = 0; i < subs->nurbs; i++) {769769- if (test_bit(i, &subs->active_mask)) {770770- if (!test_and_set_bit(i, &subs->unlink_mask)) {771771- struct urb *u = subs->dataurb[i].urb;772772- if (async)773773- usb_unlink_urb(u);774774- else775775- usb_kill_urb(u);776776- }777777- }778778- }779779- if (subs->syncpipe) {780780- for (i = 0; i < SYNC_URBS; i++) {781781- if (test_bit(i+16, &subs->active_mask)) {782782- if (!test_and_set_bit(i+16, &subs->unlink_mask)) {783783- struct urb *u = subs->syncurb[i].urb;784784- if (async)785785- usb_unlink_urb(u);786786- else787787- usb_kill_urb(u);788788- }789789- }790790- }791791- }792792- return 0;793793-}794794-795795-796796-static const char *usb_error_string(int err)797797-{798798- switch (err) {799799- case -ENODEV:800800- return "no device";801801- case -ENOENT:802802- return "endpoint not enabled";803803- case -EPIPE:804804- return "endpoint stalled";805805- case -ENOSPC:806806- return "not enough bandwidth";807807- case -ESHUTDOWN:808808- return "device disabled";809809- case -EHOSTUNREACH:810810- return "device suspended";811811- case -EINVAL:812812- case -EAGAIN:813813- case -EFBIG:814814- case -EMSGSIZE:815815- return "internal error";816816- default:817817- return "unknown error";818818- }819819-}820820-821821-/*822822- * set up and start data/sync urbs823823- */824824-static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)825825-{826826- unsigned int i;827827- int err;828828-829829- if (subs->stream->chip->shutdown)830830- return -EBADFD;831831-832832- for (i = 0; i < subs->nurbs; i++) {833833- if (snd_BUG_ON(!subs->dataurb[i].urb))834834- return -EINVAL;835835- if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {836836- snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);837837- goto __error;838838- }839839- }840840- if (subs->syncpipe) {841841- for (i = 0; i < SYNC_URBS; i++) {842842- if (snd_BUG_ON(!subs->syncurb[i].urb))843843- return -EINVAL;844844- if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {845845- snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);846846- goto __error;847847- }848848- }849849- }850850-851851- subs->active_mask = 0;852852- subs->unlink_mask = 0;853853- subs->running = 1;854854- for (i = 0; i < subs->nurbs; i++) {855855- err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);856856- if (err < 0) {857857- snd_printk(KERN_ERR "cannot submit datapipe "858858- "for urb %d, error %d: %s\n",859859- i, err, usb_error_string(err));860860- goto __error;861861- }862862- set_bit(i, &subs->active_mask);863863- }864864- if (subs->syncpipe) {865865- for (i = 0; i < SYNC_URBS; i++) {866866- err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);867867- if (err < 0) {868868- snd_printk(KERN_ERR "cannot submit syncpipe "869869- "for urb %d, error %d: %s\n",870870- i, err, usb_error_string(err));871871- goto __error;872872- }873873- set_bit(i + 16, &subs->active_mask);874874- }875875- }876876- return 0;877877-878878- __error:879879- // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);880880- deactivate_urbs(subs, 0, 0);881881- return -EPIPE;882882-}883883-884884-885885-/*886886- * wait until all urbs are processed.887887- */888888-static int wait_clear_urbs(struct snd_usb_substream *subs)889889-{890890- unsigned long end_time = jiffies + msecs_to_jiffies(1000);891891- unsigned int i;892892- int alive;893893-894894- do {895895- alive = 0;896896- for (i = 0; i < subs->nurbs; i++) {897897- if (test_bit(i, &subs->active_mask))898898- alive++;899899- }900900- if (subs->syncpipe) {901901- for (i = 0; i < SYNC_URBS; i++) {902902- if (test_bit(i + 16, &subs->active_mask))903903- alive++;904904- }905905- }906906- if (! alive)907907- break;908908- schedule_timeout_uninterruptible(1);909909- } while (time_before(jiffies, end_time));910910- if (alive)911911- snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);912912- return 0;913913-}914914-915915-916916-/*917917- * return the current pcm pointer. just based on the hwptr_done value.918918- */919919-static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)920920-{921921- struct snd_usb_substream *subs;922922- unsigned int hwptr_done;923923-924924- subs = (struct snd_usb_substream *)substream->runtime->private_data;925925- spin_lock(&subs->lock);926926- hwptr_done = subs->hwptr_done;927927- spin_unlock(&subs->lock);928928- return hwptr_done / (substream->runtime->frame_bits >> 3);929929-}930930-931931-932932-/*933933- * start/stop playback substream934934- */935935-static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,936936- int cmd)937937-{938938- struct snd_usb_substream *subs = substream->runtime->private_data;939939-940940- switch (cmd) {941941- case SNDRV_PCM_TRIGGER_START:942942- case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:943943- subs->ops.prepare = prepare_playback_urb;944944- return 0;945945- case SNDRV_PCM_TRIGGER_STOP:946946- return deactivate_urbs(subs, 0, 0);947947- case SNDRV_PCM_TRIGGER_PAUSE_PUSH:948948- subs->ops.prepare = prepare_nodata_playback_urb;949949- return 0;950950- default:951951- return -EINVAL;952952- }953953-}954954-955955-/*956956- * start/stop capture substream957957- */958958-static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,959959- int cmd)960960-{961961- struct snd_usb_substream *subs = substream->runtime->private_data;962962-963963- switch (cmd) {964964- case SNDRV_PCM_TRIGGER_START:965965- subs->ops.retire = retire_capture_urb;966966- return start_urbs(subs, substream->runtime);967967- case SNDRV_PCM_TRIGGER_STOP:968968- return deactivate_urbs(subs, 0, 0);969969- case SNDRV_PCM_TRIGGER_PAUSE_PUSH:970970- subs->ops.retire = retire_paused_capture_urb;971971- return 0;972972- case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:973973- subs->ops.retire = retire_capture_urb;974974- return 0;975975- default:976976- return -EINVAL;977977- }978978-}979979-980980-981981-/*982982- * release a urb data983983- */984984-static void release_urb_ctx(struct snd_urb_ctx *u)985985-{986986- if (u->urb) {987987- if (u->buffer_size)988988- usb_buffer_free(u->subs->dev, u->buffer_size,989989- u->urb->transfer_buffer,990990- u->urb->transfer_dma);991991- usb_free_urb(u->urb);992992- u->urb = NULL;993993- }994994-}995995-996996-/*997997- * release a substream998998- */999999-static void release_substream_urbs(struct snd_usb_substream *subs, int force)10001000-{10011001- int i;10021002-10031003- /* stop urbs (to be sure) */10041004- deactivate_urbs(subs, force, 1);10051005- wait_clear_urbs(subs);10061006-10071007- for (i = 0; i < MAX_URBS; i++)10081008- release_urb_ctx(&subs->dataurb[i]);10091009- for (i = 0; i < SYNC_URBS; i++)10101010- release_urb_ctx(&subs->syncurb[i]);10111011- usb_buffer_free(subs->dev, SYNC_URBS * 4,10121012- subs->syncbuf, subs->sync_dma);10131013- subs->syncbuf = NULL;10141014- subs->nurbs = 0;10151015-}10161016-10171017-/*10181018- * initialize a substream for plaback/capture10191019- */10201020-static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,10211021- unsigned int rate, unsigned int frame_bits)10221022-{10231023- unsigned int maxsize, i;10241024- int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;10251025- unsigned int urb_packs, total_packs, packs_per_ms;10261026-10271027- /* calculate the frequency in 16.16 format */10281028- if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)10291029- subs->freqn = get_usb_full_speed_rate(rate);10301030- else10311031- subs->freqn = get_usb_high_speed_rate(rate);10321032- subs->freqm = subs->freqn;10331033- /* calculate max. frequency */10341034- if (subs->maxpacksize) {10351035- /* whatever fits into a max. size packet */10361036- maxsize = subs->maxpacksize;10371037- subs->freqmax = (maxsize / (frame_bits >> 3))10381038- << (16 - subs->datainterval);10391039- } else {10401040- /* no max. packet size: just take 25% higher than nominal */10411041- subs->freqmax = subs->freqn + (subs->freqn >> 2);10421042- maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))10431043- >> (16 - subs->datainterval);10441044- }10451045- subs->phase = 0;10461046-10471047- if (subs->fill_max)10481048- subs->curpacksize = subs->maxpacksize;10491049- else10501050- subs->curpacksize = maxsize;10511051-10521052- if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)10531053- packs_per_ms = 8 >> subs->datainterval;10541054- else10551055- packs_per_ms = 1;10561056-10571057- if (is_playback) {10581058- urb_packs = max(nrpacks, 1);10591059- urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);10601060- } else10611061- urb_packs = 1;10621062- urb_packs *= packs_per_ms;10631063- if (subs->syncpipe)10641064- urb_packs = min(urb_packs, 1U << subs->syncinterval);10651065-10661066- /* decide how many packets to be used */10671067- if (is_playback) {10681068- unsigned int minsize, maxpacks;10691069- /* determine how small a packet can be */10701070- minsize = (subs->freqn >> (16 - subs->datainterval))10711071- * (frame_bits >> 3);10721072- /* with sync from device, assume it can be 12% lower */10731073- if (subs->syncpipe)10741074- minsize -= minsize >> 3;10751075- minsize = max(minsize, 1u);10761076- total_packs = (period_bytes + minsize - 1) / minsize;10771077- /* we need at least two URBs for queueing */10781078- if (total_packs < 2) {10791079- total_packs = 2;10801080- } else {10811081- /* and we don't want too long a queue either */10821082- maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);10831083- total_packs = min(total_packs, maxpacks);10841084- }10851085- } else {10861086- while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)10871087- urb_packs >>= 1;10881088- total_packs = MAX_URBS * urb_packs;10891089- }10901090- subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;10911091- if (subs->nurbs > MAX_URBS) {10921092- /* too much... */10931093- subs->nurbs = MAX_URBS;10941094- total_packs = MAX_URBS * urb_packs;10951095- } else if (subs->nurbs < 2) {10961096- /* too little - we need at least two packets10971097- * to ensure contiguous playback/capture10981098- */10991099- subs->nurbs = 2;11001100- }11011101-11021102- /* allocate and initialize data urbs */11031103- for (i = 0; i < subs->nurbs; i++) {11041104- struct snd_urb_ctx *u = &subs->dataurb[i];11051105- u->index = i;11061106- u->subs = subs;11071107- u->packets = (i + 1) * total_packs / subs->nurbs11081108- - i * total_packs / subs->nurbs;11091109- u->buffer_size = maxsize * u->packets;11101110- if (subs->fmt_type == UAC_FORMAT_TYPE_II)11111111- u->packets++; /* for transfer delimiter */11121112- u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);11131113- if (!u->urb)11141114- goto out_of_memory;11151115- u->urb->transfer_buffer =11161116- usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,11171117- &u->urb->transfer_dma);11181118- if (!u->urb->transfer_buffer)11191119- goto out_of_memory;11201120- u->urb->pipe = subs->datapipe;11211121- u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;11221122- u->urb->interval = 1 << subs->datainterval;11231123- u->urb->context = u;11241124- u->urb->complete = snd_complete_urb;11251125- }11261126-11271127- if (subs->syncpipe) {11281128- /* allocate and initialize sync urbs */11291129- subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,11301130- GFP_KERNEL, &subs->sync_dma);11311131- if (!subs->syncbuf)11321132- goto out_of_memory;11331133- for (i = 0; i < SYNC_URBS; i++) {11341134- struct snd_urb_ctx *u = &subs->syncurb[i];11351135- u->index = i;11361136- u->subs = subs;11371137- u->packets = 1;11381138- u->urb = usb_alloc_urb(1, GFP_KERNEL);11391139- if (!u->urb)11401140- goto out_of_memory;11411141- u->urb->transfer_buffer = subs->syncbuf + i * 4;11421142- u->urb->transfer_dma = subs->sync_dma + i * 4;11431143- u->urb->transfer_buffer_length = 4;11441144- u->urb->pipe = subs->syncpipe;11451145- u->urb->transfer_flags = URB_ISO_ASAP |11461146- URB_NO_TRANSFER_DMA_MAP;11471147- u->urb->number_of_packets = 1;11481148- u->urb->interval = 1 << subs->syncinterval;11491149- u->urb->context = u;11501150- u->urb->complete = snd_complete_sync_urb;11511151- }11521152- }11531153- return 0;11541154-11551155-out_of_memory:11561156- release_substream_urbs(subs, 0);11571157- return -ENOMEM;11581158-}11591159-11601160-11611161-/*11621162- * find a matching audio format11631163- */11641164-static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,11651165- unsigned int rate, unsigned int channels)11661166-{11671167- struct list_head *p;11681168- struct audioformat *found = NULL;11691169- int cur_attr = 0, attr;11701170-11711171- list_for_each(p, &subs->fmt_list) {11721172- struct audioformat *fp;11731173- fp = list_entry(p, struct audioformat, list);11741174- if (fp->format != format || fp->channels != channels)11751175- continue;11761176- if (rate < fp->rate_min || rate > fp->rate_max)11771177- continue;11781178- if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {11791179- unsigned int i;11801180- for (i = 0; i < fp->nr_rates; i++)11811181- if (fp->rate_table[i] == rate)11821182- break;11831183- if (i >= fp->nr_rates)11841184- continue;11851185- }11861186- attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;11871187- if (! found) {11881188- found = fp;11891189- cur_attr = attr;11901190- continue;11911191- }11921192- /* avoid async out and adaptive in if the other method11931193- * supports the same format.11941194- * this is a workaround for the case like11951195- * M-audio audiophile USB.11961196- */11971197- if (attr != cur_attr) {11981198- if ((attr == USB_ENDPOINT_SYNC_ASYNC &&11991199- subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||12001200- (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&12011201- subs->direction == SNDRV_PCM_STREAM_CAPTURE))12021202- continue;12031203- if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&12041204- subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||12051205- (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&12061206- subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {12071207- found = fp;12081208- cur_attr = attr;12091209- continue;12101210- }12111211- }12121212- /* find the format with the largest max. packet size */12131213- if (fp->maxpacksize > found->maxpacksize) {12141214- found = fp;12151215- cur_attr = attr;12161216- }12171217- }12181218- return found;12191219-}12201220-12211221-12221222-/*12231223- * initialize the picth control and sample rate12241224- */12251225-static int init_usb_pitch(struct usb_device *dev, int iface,12261226- struct usb_host_interface *alts,12271227- struct audioformat *fmt)12281228-{12291229- unsigned int ep;12301230- unsigned char data[1];12311231- int err;12321232-12331233- ep = get_endpoint(alts, 0)->bEndpointAddress;12341234- /* if endpoint has pitch control, enable it */12351235- if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) {12361236- data[0] = 1;12371237- if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,12381238- USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,12391239- UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {12401240- snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",12411241- dev->devnum, iface, ep);12421242- return err;12431243- }12441244- }12451245- return 0;12461246-}12471247-12481248-static int init_usb_sample_rate(struct usb_device *dev, int iface,12491249- struct usb_host_interface *alts,12501250- struct audioformat *fmt, int rate)12511251-{12521252- unsigned int ep;12531253- unsigned char data[3];12541254- int err;12551255-12561256- ep = get_endpoint(alts, 0)->bEndpointAddress;12571257- /* if endpoint has sampling rate control, set it */12581258- if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) {12591259- int crate;12601260- data[0] = rate;12611261- data[1] = rate >> 8;12621262- data[2] = rate >> 16;12631263- if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,12641264- USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,12651265- UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {12661266- snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",12671267- dev->devnum, iface, fmt->altsetting, rate, ep);12681268- return err;12691269- }12701270- if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,12711271- USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,12721272- UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {12731273- snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",12741274- dev->devnum, iface, fmt->altsetting, ep);12751275- return 0; /* some devices don't support reading */12761276- }12771277- crate = data[0] | (data[1] << 8) | (data[2] << 16);12781278- if (crate != rate) {12791279- snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);12801280- // runtime->rate = crate;12811281- }12821282- }12831283- return 0;12841284-}12851285-12861286-/*12871287- * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,12881288- * not for interface.12891289- */12901290-static void set_format_emu_quirk(struct snd_usb_substream *subs,12911291- struct audioformat *fmt)12921292-{12931293- unsigned char emu_samplerate_id = 0;12941294-12951295- /* When capture is active12961296- * sample rate shouldn't be changed12971297- * by playback substream12981298- */12991299- if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {13001300- if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)13011301- return;13021302- }13031303-13041304- switch (fmt->rate_min) {13051305- case 48000:13061306- emu_samplerate_id = EMU_QUIRK_SR_48000HZ;13071307- break;13081308- case 88200:13091309- emu_samplerate_id = EMU_QUIRK_SR_88200HZ;13101310- break;13111311- case 96000:13121312- emu_samplerate_id = EMU_QUIRK_SR_96000HZ;13131313- break;13141314- case 176400:13151315- emu_samplerate_id = EMU_QUIRK_SR_176400HZ;13161316- break;13171317- case 192000:13181318- emu_samplerate_id = EMU_QUIRK_SR_192000HZ;13191319- break;13201320- default:13211321- emu_samplerate_id = EMU_QUIRK_SR_44100HZ;13221322- break;13231323- }13241324- snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);13251325-}13261326-13271327-/*13281328- * find a matching format and set up the interface13291329- */13301330-static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)13311331-{13321332- struct usb_device *dev = subs->dev;13331333- struct usb_host_interface *alts;13341334- struct usb_interface_descriptor *altsd;13351335- struct usb_interface *iface;13361336- unsigned int ep, attr;13371337- int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;13381338- int err;13391339-13401340- iface = usb_ifnum_to_if(dev, fmt->iface);13411341- if (WARN_ON(!iface))13421342- return -EINVAL;13431343- alts = &iface->altsetting[fmt->altset_idx];13441344- altsd = get_iface_desc(alts);13451345- if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))13461346- return -EINVAL;13471347-13481348- if (fmt == subs->cur_audiofmt)13491349- return 0;13501350-13511351- /* close the old interface */13521352- if (subs->interface >= 0 && subs->interface != fmt->iface) {13531353- if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {13541354- snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",13551355- dev->devnum, fmt->iface, fmt->altsetting);13561356- return -EIO;13571357- }13581358- subs->interface = -1;13591359- subs->format = 0;13601360- }13611361-13621362- /* set interface */13631363- if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {13641364- if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {13651365- snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",13661366- dev->devnum, fmt->iface, fmt->altsetting);13671367- return -EIO;13681368- }13691369- snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);13701370- subs->interface = fmt->iface;13711371- subs->format = fmt->altset_idx;13721372- }13731373-13741374- /* create a data pipe */13751375- ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;13761376- if (is_playback)13771377- subs->datapipe = usb_sndisocpipe(dev, ep);13781378- else13791379- subs->datapipe = usb_rcvisocpipe(dev, ep);13801380- subs->datainterval = fmt->datainterval;13811381- subs->syncpipe = subs->syncinterval = 0;13821382- subs->maxpacksize = fmt->maxpacksize;13831383- subs->fill_max = 0;13841384-13851385- /* we need a sync pipe in async OUT or adaptive IN mode */13861386- /* check the number of EP, since some devices have broken13871387- * descriptors which fool us. if it has only one EP,13881388- * assume it as adaptive-out or sync-in.13891389- */13901390- attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;13911391- if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||13921392- (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&13931393- altsd->bNumEndpoints >= 2) {13941394- /* check sync-pipe endpoint */13951395- /* ... and check descriptor size before accessing bSynchAddress13961396- because there is a version of the SB Audigy 2 NX firmware lacking13971397- the audio fields in the endpoint descriptors */13981398- if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||13991399- (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&14001400- get_endpoint(alts, 1)->bSynchAddress != 0)) {14011401- snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",14021402- dev->devnum, fmt->iface, fmt->altsetting);14031403- return -EINVAL;14041404- }14051405- ep = get_endpoint(alts, 1)->bEndpointAddress;14061406- if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&14071407- (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||14081408- (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {14091409- snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",14101410- dev->devnum, fmt->iface, fmt->altsetting);14111411- return -EINVAL;14121412- }14131413- ep &= USB_ENDPOINT_NUMBER_MASK;14141414- if (is_playback)14151415- subs->syncpipe = usb_rcvisocpipe(dev, ep);14161416- else14171417- subs->syncpipe = usb_sndisocpipe(dev, ep);14181418- if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&14191419- get_endpoint(alts, 1)->bRefresh >= 1 &&14201420- get_endpoint(alts, 1)->bRefresh <= 9)14211421- subs->syncinterval = get_endpoint(alts, 1)->bRefresh;14221422- else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)14231423- subs->syncinterval = 1;14241424- else if (get_endpoint(alts, 1)->bInterval >= 1 &&14251425- get_endpoint(alts, 1)->bInterval <= 16)14261426- subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;14271427- else14281428- subs->syncinterval = 3;14291429- }14301430-14311431- /* always fill max packet size */14321432- if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)14331433- subs->fill_max = 1;14341434-14351435- if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)14361436- return err;14371437-14381438- subs->cur_audiofmt = fmt;14391439-14401440- switch (subs->stream->chip->usb_id) {14411441- case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */14421442- case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */14431443- case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */14441444- set_format_emu_quirk(subs, fmt);14451445- break;14461446- }14471447-14481448-#if 014491449- printk(KERN_DEBUG14501450- "setting done: format = %d, rate = %d..%d, channels = %d\n",14511451- fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);14521452- printk(KERN_DEBUG14531453- " datapipe = 0x%0x, syncpipe = 0x%0x\n",14541454- subs->datapipe, subs->syncpipe);14551455-#endif14561456-14571457- return 0;14581458-}14591459-14601460-/*14611461- * hw_params callback14621462- *14631463- * allocate a buffer and set the given audio format.14641464- *14651465- * so far we use a physically linear buffer although packetize transfer14661466- * doesn't need a continuous area.14671467- * if sg buffer is supported on the later version of alsa, we'll follow14681468- * that.14691469- */14701470-static int snd_usb_hw_params(struct snd_pcm_substream *substream,14711471- struct snd_pcm_hw_params *hw_params)14721472-{14731473- struct snd_usb_substream *subs = substream->runtime->private_data;14741474- struct audioformat *fmt;14751475- unsigned int channels, rate, format;14761476- int ret, changed;14771477-14781478- ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,14791479- params_buffer_bytes(hw_params));14801480- if (ret < 0)14811481- return ret;14821482-14831483- format = params_format(hw_params);14841484- rate = params_rate(hw_params);14851485- channels = params_channels(hw_params);14861486- fmt = find_format(subs, format, rate, channels);14871487- if (!fmt) {14881488- snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",14891489- format, rate, channels);14901490- return -EINVAL;14911491- }14921492-14931493- changed = subs->cur_audiofmt != fmt ||14941494- subs->period_bytes != params_period_bytes(hw_params) ||14951495- subs->cur_rate != rate;14961496- if ((ret = set_format(subs, fmt)) < 0)14971497- return ret;14981498-14991499- if (subs->cur_rate != rate) {15001500- struct usb_host_interface *alts;15011501- struct usb_interface *iface;15021502- iface = usb_ifnum_to_if(subs->dev, fmt->iface);15031503- alts = &iface->altsetting[fmt->altset_idx];15041504- ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);15051505- if (ret < 0)15061506- return ret;15071507- subs->cur_rate = rate;15081508- }15091509-15101510- if (changed) {15111511- /* format changed */15121512- release_substream_urbs(subs, 0);15131513- /* influenced: period_bytes, channels, rate, format, */15141514- ret = init_substream_urbs(subs, params_period_bytes(hw_params),15151515- params_rate(hw_params),15161516- snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));15171517- }15181518-15191519- return ret;15201520-}15211521-15221522-/*15231523- * hw_free callback15241524- *15251525- * reset the audio format and release the buffer15261526- */15271527-static int snd_usb_hw_free(struct snd_pcm_substream *substream)15281528-{15291529- struct snd_usb_substream *subs = substream->runtime->private_data;15301530-15311531- subs->cur_audiofmt = NULL;15321532- subs->cur_rate = 0;15331533- subs->period_bytes = 0;15341534- if (!subs->stream->chip->shutdown)15351535- release_substream_urbs(subs, 0);15361536- return snd_pcm_lib_free_vmalloc_buffer(substream);15371537-}15381538-15391539-/*15401540- * prepare callback15411541- *15421542- * only a few subtle things...15431543- */15441544-static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)15451545-{15461546- struct snd_pcm_runtime *runtime = substream->runtime;15471547- struct snd_usb_substream *subs = runtime->private_data;15481548-15491549- if (! subs->cur_audiofmt) {15501550- snd_printk(KERN_ERR "usbaudio: no format is specified!\n");15511551- return -ENXIO;15521552- }15531553-15541554- /* some unit conversions in runtime */15551555- subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);15561556- subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);15571557-15581558- /* reset the pointer */15591559- subs->hwptr_done = 0;15601560- subs->transfer_done = 0;15611561- subs->phase = 0;15621562- runtime->delay = 0;15631563-15641564- /* clear urbs (to be sure) */15651565- deactivate_urbs(subs, 0, 1);15661566- wait_clear_urbs(subs);15671567-15681568- /* for playback, submit the URBs now; otherwise, the first hwptr_done15691569- * updates for all URBs would happen at the same time when starting */15701570- if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {15711571- subs->ops.prepare = prepare_nodata_playback_urb;15721572- return start_urbs(subs, runtime);15731573- } else15741574- return 0;15751575-}15761576-15771577-static struct snd_pcm_hardware snd_usb_hardware =15781578-{15791579- .info = SNDRV_PCM_INFO_MMAP |15801580- SNDRV_PCM_INFO_MMAP_VALID |15811581- SNDRV_PCM_INFO_BATCH |15821582- SNDRV_PCM_INFO_INTERLEAVED |15831583- SNDRV_PCM_INFO_BLOCK_TRANSFER |15841584- SNDRV_PCM_INFO_PAUSE,15851585- .buffer_bytes_max = 1024 * 1024,15861586- .period_bytes_min = 64,15871587- .period_bytes_max = 512 * 1024,15881588- .periods_min = 2,15891589- .periods_max = 1024,15901590-};15911591-15921592-/*15931593- * h/w constraints15941594- */15951595-15961596-#ifdef HW_CONST_DEBUG15971597-#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)15981598-#else15991599-#define hwc_debug(fmt, args...) /**/16001600-#endif16011601-16021602-static int hw_check_valid_format(struct snd_usb_substream *subs,16031603- struct snd_pcm_hw_params *params,16041604- struct audioformat *fp)16051605-{16061606- struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);16071607- struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);16081608- struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);16091609- struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);16101610- unsigned int ptime;16111611-16121612- /* check the format */16131613- if (!snd_mask_test(fmts, fp->format)) {16141614- hwc_debug(" > check: no supported format %d\n", fp->format);16151615- return 0;16161616- }16171617- /* check the channels */16181618- if (fp->channels < ct->min || fp->channels > ct->max) {16191619- hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);16201620- return 0;16211621- }16221622- /* check the rate is within the range */16231623- if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {16241624- hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);16251625- return 0;16261626- }16271627- if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {16281628- hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);16291629- return 0;16301630- }16311631- /* check whether the period time is >= the data packet interval */16321632- if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {16331633- ptime = 125 * (1 << fp->datainterval);16341634- if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {16351635- hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);16361636- return 0;16371637- }16381638- }16391639- return 1;16401640-}16411641-16421642-static int hw_rule_rate(struct snd_pcm_hw_params *params,16431643- struct snd_pcm_hw_rule *rule)16441644-{16451645- struct snd_usb_substream *subs = rule->private;16461646- struct list_head *p;16471647- struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);16481648- unsigned int rmin, rmax;16491649- int changed;16501650-16511651- hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);16521652- changed = 0;16531653- rmin = rmax = 0;16541654- list_for_each(p, &subs->fmt_list) {16551655- struct audioformat *fp;16561656- fp = list_entry(p, struct audioformat, list);16571657- if (!hw_check_valid_format(subs, params, fp))16581658- continue;16591659- if (changed++) {16601660- if (rmin > fp->rate_min)16611661- rmin = fp->rate_min;16621662- if (rmax < fp->rate_max)16631663- rmax = fp->rate_max;16641664- } else {16651665- rmin = fp->rate_min;16661666- rmax = fp->rate_max;16671667- }16681668- }16691669-16701670- if (!changed) {16711671- hwc_debug(" --> get empty\n");16721672- it->empty = 1;16731673- return -EINVAL;16741674- }16751675-16761676- changed = 0;16771677- if (it->min < rmin) {16781678- it->min = rmin;16791679- it->openmin = 0;16801680- changed = 1;16811681- }16821682- if (it->max > rmax) {16831683- it->max = rmax;16841684- it->openmax = 0;16851685- changed = 1;16861686- }16871687- if (snd_interval_checkempty(it)) {16881688- it->empty = 1;16891689- return -EINVAL;16901690- }16911691- hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);16921692- return changed;16931693-}16941694-16951695-16961696-static int hw_rule_channels(struct snd_pcm_hw_params *params,16971697- struct snd_pcm_hw_rule *rule)16981698-{16991699- struct snd_usb_substream *subs = rule->private;17001700- struct list_head *p;17011701- struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);17021702- unsigned int rmin, rmax;17031703- int changed;17041704-17051705- hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);17061706- changed = 0;17071707- rmin = rmax = 0;17081708- list_for_each(p, &subs->fmt_list) {17091709- struct audioformat *fp;17101710- fp = list_entry(p, struct audioformat, list);17111711- if (!hw_check_valid_format(subs, params, fp))17121712- continue;17131713- if (changed++) {17141714- if (rmin > fp->channels)17151715- rmin = fp->channels;17161716- if (rmax < fp->channels)17171717- rmax = fp->channels;17181718- } else {17191719- rmin = fp->channels;17201720- rmax = fp->channels;17211721- }17221722- }17231723-17241724- if (!changed) {17251725- hwc_debug(" --> get empty\n");17261726- it->empty = 1;17271727- return -EINVAL;17281728- }17291729-17301730- changed = 0;17311731- if (it->min < rmin) {17321732- it->min = rmin;17331733- it->openmin = 0;17341734- changed = 1;17351735- }17361736- if (it->max > rmax) {17371737- it->max = rmax;17381738- it->openmax = 0;17391739- changed = 1;17401740- }17411741- if (snd_interval_checkempty(it)) {17421742- it->empty = 1;17431743- return -EINVAL;17441744- }17451745- hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);17461746- return changed;17471747-}17481748-17491749-static int hw_rule_format(struct snd_pcm_hw_params *params,17501750- struct snd_pcm_hw_rule *rule)17511751-{17521752- struct snd_usb_substream *subs = rule->private;17531753- struct list_head *p;17541754- struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);17551755- u64 fbits;17561756- u32 oldbits[2];17571757- int changed;17581758-17591759- hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);17601760- fbits = 0;17611761- list_for_each(p, &subs->fmt_list) {17621762- struct audioformat *fp;17631763- fp = list_entry(p, struct audioformat, list);17641764- if (!hw_check_valid_format(subs, params, fp))17651765- continue;17661766- fbits |= (1ULL << fp->format);17671767- }17681768-17691769- oldbits[0] = fmt->bits[0];17701770- oldbits[1] = fmt->bits[1];17711771- fmt->bits[0] &= (u32)fbits;17721772- fmt->bits[1] &= (u32)(fbits >> 32);17731773- if (!fmt->bits[0] && !fmt->bits[1]) {17741774- hwc_debug(" --> get empty\n");17751775- return -EINVAL;17761776- }17771777- changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);17781778- hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);17791779- return changed;17801780-}17811781-17821782-static int hw_rule_period_time(struct snd_pcm_hw_params *params,17831783- struct snd_pcm_hw_rule *rule)17841784-{17851785- struct snd_usb_substream *subs = rule->private;17861786- struct audioformat *fp;17871787- struct snd_interval *it;17881788- unsigned char min_datainterval;17891789- unsigned int pmin;17901790- int changed;17911791-17921792- it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);17931793- hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);17941794- min_datainterval = 0xff;17951795- list_for_each_entry(fp, &subs->fmt_list, list) {17961796- if (!hw_check_valid_format(subs, params, fp))17971797- continue;17981798- min_datainterval = min(min_datainterval, fp->datainterval);17991799- }18001800- if (min_datainterval == 0xff) {18011801- hwc_debug(" --> get emtpy\n");18021802- it->empty = 1;18031803- return -EINVAL;18041804- }18051805- pmin = 125 * (1 << min_datainterval);18061806- changed = 0;18071807- if (it->min < pmin) {18081808- it->min = pmin;18091809- it->openmin = 0;18101810- changed = 1;18111811- }18121812- if (snd_interval_checkempty(it)) {18131813- it->empty = 1;18141814- return -EINVAL;18151815- }18161816- hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);18171817- return changed;18181818-}18191819-18201820-/*18211821- * If the device supports unusual bit rates, does the request meet these?18221822- */18231823-static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,18241824- struct snd_usb_substream *subs)18251825-{18261826- struct audioformat *fp;18271827- int count = 0, needs_knot = 0;18281828- int err;18291829-18301830- list_for_each_entry(fp, &subs->fmt_list, list) {18311831- if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)18321832- return 0;18331833- count += fp->nr_rates;18341834- if (fp->rates & SNDRV_PCM_RATE_KNOT)18351835- needs_knot = 1;18361836- }18371837- if (!needs_knot)18381838- return 0;18391839-18401840- subs->rate_list.count = count;18411841- subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);18421842- subs->rate_list.mask = 0;18431843- count = 0;18441844- list_for_each_entry(fp, &subs->fmt_list, list) {18451845- int i;18461846- for (i = 0; i < fp->nr_rates; i++)18471847- subs->rate_list.list[count++] = fp->rate_table[i];18481848- }18491849- err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,18501850- &subs->rate_list);18511851- if (err < 0)18521852- return err;18531853-18541854- return 0;18551855-}18561856-18571857-18581858-/*18591859- * set up the runtime hardware information.18601860- */18611861-18621862-static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)18631863-{18641864- struct list_head *p;18651865- unsigned int pt, ptmin;18661866- int param_period_time_if_needed;18671867- int err;18681868-18691869- runtime->hw.formats = subs->formats;18701870-18711871- runtime->hw.rate_min = 0x7fffffff;18721872- runtime->hw.rate_max = 0;18731873- runtime->hw.channels_min = 256;18741874- runtime->hw.channels_max = 0;18751875- runtime->hw.rates = 0;18761876- ptmin = UINT_MAX;18771877- /* check min/max rates and channels */18781878- list_for_each(p, &subs->fmt_list) {18791879- struct audioformat *fp;18801880- fp = list_entry(p, struct audioformat, list);18811881- runtime->hw.rates |= fp->rates;18821882- if (runtime->hw.rate_min > fp->rate_min)18831883- runtime->hw.rate_min = fp->rate_min;18841884- if (runtime->hw.rate_max < fp->rate_max)18851885- runtime->hw.rate_max = fp->rate_max;18861886- if (runtime->hw.channels_min > fp->channels)18871887- runtime->hw.channels_min = fp->channels;18881888- if (runtime->hw.channels_max < fp->channels)18891889- runtime->hw.channels_max = fp->channels;18901890- if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {18911891- /* FIXME: there might be more than one audio formats... */18921892- runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =18931893- fp->frame_size;18941894- }18951895- pt = 125 * (1 << fp->datainterval);18961896- ptmin = min(ptmin, pt);18971897- }18981898-18991899- param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;19001900- if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)19011901- /* full speed devices have fixed data packet interval */19021902- ptmin = 1000;19031903- if (ptmin == 1000)19041904- /* if period time doesn't go below 1 ms, no rules needed */19051905- param_period_time_if_needed = -1;19061906- snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,19071907- ptmin, UINT_MAX);19081908-19091909- if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,19101910- hw_rule_rate, subs,19111911- SNDRV_PCM_HW_PARAM_FORMAT,19121912- SNDRV_PCM_HW_PARAM_CHANNELS,19131913- param_period_time_if_needed,19141914- -1)) < 0)19151915- return err;19161916- if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,19171917- hw_rule_channels, subs,19181918- SNDRV_PCM_HW_PARAM_FORMAT,19191919- SNDRV_PCM_HW_PARAM_RATE,19201920- param_period_time_if_needed,19211921- -1)) < 0)19221922- return err;19231923- if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,19241924- hw_rule_format, subs,19251925- SNDRV_PCM_HW_PARAM_RATE,19261926- SNDRV_PCM_HW_PARAM_CHANNELS,19271927- param_period_time_if_needed,19281928- -1)) < 0)19291929- return err;19301930- if (param_period_time_if_needed >= 0) {19311931- err = snd_pcm_hw_rule_add(runtime, 0,19321932- SNDRV_PCM_HW_PARAM_PERIOD_TIME,19331933- hw_rule_period_time, subs,19341934- SNDRV_PCM_HW_PARAM_FORMAT,19351935- SNDRV_PCM_HW_PARAM_CHANNELS,19361936- SNDRV_PCM_HW_PARAM_RATE,19371937- -1);19381938- if (err < 0)19391939- return err;19401940- }19411941- if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)19421942- return err;19431943- return 0;19441944-}19451945-19461946-static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)19471947-{19481948- struct snd_usb_stream *as = snd_pcm_substream_chip(substream);19491949- struct snd_pcm_runtime *runtime = substream->runtime;19501950- struct snd_usb_substream *subs = &as->substream[direction];19511951-19521952- subs->interface = -1;19531953- subs->format = 0;19541954- runtime->hw = snd_usb_hardware;19551955- runtime->private_data = subs;19561956- subs->pcm_substream = substream;19571957- return setup_hw_info(runtime, subs);19581958-}19591959-19601960-static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)19611961-{19621962- struct snd_usb_stream *as = snd_pcm_substream_chip(substream);19631963- struct snd_usb_substream *subs = &as->substream[direction];19641964-19651965- if (!as->chip->shutdown && subs->interface >= 0) {19661966- usb_set_interface(subs->dev, subs->interface, 0);19671967- subs->interface = -1;19681968- }19691969- subs->pcm_substream = NULL;19701970- return 0;19711971-}19721972-19731973-static int snd_usb_playback_open(struct snd_pcm_substream *substream)19741974-{19751975- return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);19761976-}19771977-19781978-static int snd_usb_playback_close(struct snd_pcm_substream *substream)19791979-{19801980- return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);19811981-}19821982-19831983-static int snd_usb_capture_open(struct snd_pcm_substream *substream)19841984-{19851985- return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);19861986-}19871987-19881988-static int snd_usb_capture_close(struct snd_pcm_substream *substream)19891989-{19901990- return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);19911991-}19921992-19931993-static struct snd_pcm_ops snd_usb_playback_ops = {19941994- .open = snd_usb_playback_open,19951995- .close = snd_usb_playback_close,19961996- .ioctl = snd_pcm_lib_ioctl,19971997- .hw_params = snd_usb_hw_params,19981998- .hw_free = snd_usb_hw_free,19991999- .prepare = snd_usb_pcm_prepare,20002000- .trigger = snd_usb_pcm_playback_trigger,20012001- .pointer = snd_usb_pcm_pointer,20022002- .page = snd_pcm_lib_get_vmalloc_page,20032003- .mmap = snd_pcm_lib_mmap_vmalloc,20042004-};20052005-20062006-static struct snd_pcm_ops snd_usb_capture_ops = {20072007- .open = snd_usb_capture_open,20082008- .close = snd_usb_capture_close,20092009- .ioctl = snd_pcm_lib_ioctl,20102010- .hw_params = snd_usb_hw_params,20112011- .hw_free = snd_usb_hw_free,20122012- .prepare = snd_usb_pcm_prepare,20132013- .trigger = snd_usb_pcm_capture_trigger,20142014- .pointer = snd_usb_pcm_pointer,20152015- .page = snd_pcm_lib_get_vmalloc_page,20162016- .mmap = snd_pcm_lib_mmap_vmalloc,20172017-};20182018-20192019-20202020-20212021-/*20222022- * helper functions20232023- */20242024-20252025-/*20262026- * combine bytes and get an integer value20272027- */20282028-unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)20292029-{20302030- switch (size) {20312031- case 1: return *bytes;20322032- case 2: return combine_word(bytes);20332033- case 3: return combine_triple(bytes);20342034- case 4: return combine_quad(bytes);20352035- default: return 0;20362036- }20372037-}20382038-20392039-/*20402040- * parse descriptor buffer and return the pointer starting the given20412041- * descriptor type.20422042- */20432043-void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)20442044-{20452045- u8 *p, *end, *next;20462046-20472047- p = descstart;20482048- end = p + desclen;20492049- for (; p < end;) {20502050- if (p[0] < 2)20512051- return NULL;20522052- next = p + p[0];20532053- if (next > end)20542054- return NULL;20552055- if (p[1] == dtype && (!after || (void *)p > after)) {20562056- return p;20572057- }20582058- p = next;20592059- }20602060- return NULL;20612061-}20622062-20632063-/*20642064- * find a class-specified interface descriptor with the given subtype.20652065- */20662066-void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)20672067-{20682068- unsigned char *p = after;20692069-20702070- while ((p = snd_usb_find_desc(buffer, buflen, p,20712071- USB_DT_CS_INTERFACE)) != NULL) {20722072- if (p[0] >= 3 && p[2] == dsubtype)20732073- return p;20742074- }20752075- return NULL;20762076-}20772077-20782078-/*20792079- * Wrapper for usb_control_msg().20802080- * Allocates a temp buffer to prevent dmaing from/to the stack.20812081- */20822082-int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,20832083- __u8 requesttype, __u16 value, __u16 index, void *data,20842084- __u16 size, int timeout)20852085-{20862086- int err;20872087- void *buf = NULL;20882088-20892089- if (size > 0) {20902090- buf = kmemdup(data, size, GFP_KERNEL);20912091- if (!buf)20922092- return -ENOMEM;20932093- }20942094- err = usb_control_msg(dev, pipe, request, requesttype,20952095- value, index, buf, size, timeout);20962096- if (size > 0) {20972097- memcpy(data, buf, size);20982098- kfree(buf);20992099- }21002100- return err;21012101-}21022102-21032103-21042104-/*21052105- * entry point for linux usb interface21062106- */21072107-21082108-static int usb_audio_probe(struct usb_interface *intf,21092109- const struct usb_device_id *id);21102110-static void usb_audio_disconnect(struct usb_interface *intf);21112111-21122112-#ifdef CONFIG_PM21132113-static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message);21142114-static int usb_audio_resume(struct usb_interface *intf);21152115-#else21162116-#define usb_audio_suspend NULL21172117-#define usb_audio_resume NULL21182118-#endif21192119-21202120-static struct usb_device_id usb_audio_ids [] = {21212121-#include "usbquirks.h"21222122- { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),21232123- .bInterfaceClass = USB_CLASS_AUDIO,21242124- .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },21252125- { } /* Terminating entry */21262126-};21272127-21282128-MODULE_DEVICE_TABLE (usb, usb_audio_ids);21292129-21302130-static struct usb_driver usb_audio_driver = {21312131- .name = "snd-usb-audio",21322132- .probe = usb_audio_probe,21332133- .disconnect = usb_audio_disconnect,21342134- .suspend = usb_audio_suspend,21352135- .resume = usb_audio_resume,21362136- .id_table = usb_audio_ids,21372137-};21382138-21392139-21402140-#if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)21412141-21422142-/*21432143- * proc interface for list the supported pcm formats21442144- */21452145-static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)21462146-{21472147- struct list_head *p;21482148- static char *sync_types[4] = {21492149- "NONE", "ASYNC", "ADAPTIVE", "SYNC"21502150- };21512151-21522152- list_for_each(p, &subs->fmt_list) {21532153- struct audioformat *fp;21542154- fp = list_entry(p, struct audioformat, list);21552155- snd_iprintf(buffer, " Interface %d\n", fp->iface);21562156- snd_iprintf(buffer, " Altset %d\n", fp->altsetting);21572157- snd_iprintf(buffer, " Format: %s\n",21582158- snd_pcm_format_name(fp->format));21592159- snd_iprintf(buffer, " Channels: %d\n", fp->channels);21602160- snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",21612161- fp->endpoint & USB_ENDPOINT_NUMBER_MASK,21622162- fp->endpoint & USB_DIR_IN ? "IN" : "OUT",21632163- sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);21642164- if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {21652165- snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",21662166- fp->rate_min, fp->rate_max);21672167- } else {21682168- unsigned int i;21692169- snd_iprintf(buffer, " Rates: ");21702170- for (i = 0; i < fp->nr_rates; i++) {21712171- if (i > 0)21722172- snd_iprintf(buffer, ", ");21732173- snd_iprintf(buffer, "%d", fp->rate_table[i]);21742174- }21752175- snd_iprintf(buffer, "\n");21762176- }21772177- if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)21782178- snd_iprintf(buffer, " Data packet interval: %d us\n",21792179- 125 * (1 << fp->datainterval));21802180- // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);21812181- // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);21822182- }21832183-}21842184-21852185-static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)21862186-{21872187- if (subs->running) {21882188- unsigned int i;21892189- snd_iprintf(buffer, " Status: Running\n");21902190- snd_iprintf(buffer, " Interface = %d\n", subs->interface);21912191- snd_iprintf(buffer, " Altset = %d\n", subs->format);21922192- snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);21932193- for (i = 0; i < subs->nurbs; i++)21942194- snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);21952195- snd_iprintf(buffer, "]\n");21962196- snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);21972197- snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",21982198- snd_usb_get_speed(subs->dev) == USB_SPEED_FULL21992199- ? get_full_speed_hz(subs->freqm)22002200- : get_high_speed_hz(subs->freqm),22012201- subs->freqm >> 16, subs->freqm & 0xffff);22022202- } else {22032203- snd_iprintf(buffer, " Status: Stop\n");22042204- }22052205-}22062206-22072207-static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)22082208-{22092209- struct snd_usb_stream *stream = entry->private_data;22102210-22112211- snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);22122212-22132213- if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {22142214- snd_iprintf(buffer, "\nPlayback:\n");22152215- proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);22162216- proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);22172217- }22182218- if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {22192219- snd_iprintf(buffer, "\nCapture:\n");22202220- proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);22212221- proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);22222222- }22232223-}22242224-22252225-static void proc_pcm_format_add(struct snd_usb_stream *stream)22262226-{22272227- struct snd_info_entry *entry;22282228- char name[32];22292229- struct snd_card *card = stream->chip->card;22302230-22312231- sprintf(name, "stream%d", stream->pcm_index);22322232- if (!snd_card_proc_new(card, name, &entry))22332233- snd_info_set_text_ops(entry, stream, proc_pcm_format_read);22342234-}22352235-22362236-#else22372237-22382238-static inline void proc_pcm_format_add(struct snd_usb_stream *stream)22392239-{22402240-}22412241-22422242-#endif22432243-22442244-/*22452245- * initialize the substream instance.22462246- */22472247-22482248-static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)22492249-{22502250- struct snd_usb_substream *subs = &as->substream[stream];22512251-22522252- INIT_LIST_HEAD(&subs->fmt_list);22532253- spin_lock_init(&subs->lock);22542254-22552255- subs->stream = as;22562256- subs->direction = stream;22572257- subs->dev = as->chip->dev;22582258- subs->txfr_quirk = as->chip->txfr_quirk;22592259- if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {22602260- subs->ops = audio_urb_ops[stream];22612261- } else {22622262- subs->ops = audio_urb_ops_high_speed[stream];22632263- switch (as->chip->usb_id) {22642264- case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */22652265- case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */22662266- case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */22672267- subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;22682268- break;22692269- }22702270- }22712271- snd_pcm_set_ops(as->pcm, stream,22722272- stream == SNDRV_PCM_STREAM_PLAYBACK ?22732273- &snd_usb_playback_ops : &snd_usb_capture_ops);22742274-22752275- list_add_tail(&fp->list, &subs->fmt_list);22762276- subs->formats |= 1ULL << fp->format;22772277- subs->endpoint = fp->endpoint;22782278- subs->num_formats++;22792279- subs->fmt_type = fp->fmt_type;22802280-}22812281-22822282-22832283-/*22842284- * free a substream22852285- */22862286-static void free_substream(struct snd_usb_substream *subs)22872287-{22882288- struct list_head *p, *n;22892289-22902290- if (!subs->num_formats)22912291- return; /* not initialized */22922292- list_for_each_safe(p, n, &subs->fmt_list) {22932293- struct audioformat *fp = list_entry(p, struct audioformat, list);22942294- kfree(fp->rate_table);22952295- kfree(fp);22962296- }22972297- kfree(subs->rate_list.list);22982298-}22992299-23002300-23012301-/*23022302- * free a usb stream instance23032303- */23042304-static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)23052305-{23062306- free_substream(&stream->substream[0]);23072307- free_substream(&stream->substream[1]);23082308- list_del(&stream->list);23092309- kfree(stream);23102310-}23112311-23122312-static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)23132313-{23142314- struct snd_usb_stream *stream = pcm->private_data;23152315- if (stream) {23162316- stream->pcm = NULL;23172317- snd_usb_audio_stream_free(stream);23182318- }23192319-}23202320-23212321-23222322-/*23232323- * add this endpoint to the chip instance.23242324- * if a stream with the same endpoint already exists, append to it.23252325- * if not, create a new pcm stream.23262326- */23272327-static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)23282328-{23292329- struct list_head *p;23302330- struct snd_usb_stream *as;23312331- struct snd_usb_substream *subs;23322332- struct snd_pcm *pcm;23332333- int err;23342334-23352335- list_for_each(p, &chip->pcm_list) {23362336- as = list_entry(p, struct snd_usb_stream, list);23372337- if (as->fmt_type != fp->fmt_type)23382338- continue;23392339- subs = &as->substream[stream];23402340- if (!subs->endpoint)23412341- continue;23422342- if (subs->endpoint == fp->endpoint) {23432343- list_add_tail(&fp->list, &subs->fmt_list);23442344- subs->num_formats++;23452345- subs->formats |= 1ULL << fp->format;23462346- return 0;23472347- }23482348- }23492349- /* look for an empty stream */23502350- list_for_each(p, &chip->pcm_list) {23512351- as = list_entry(p, struct snd_usb_stream, list);23522352- if (as->fmt_type != fp->fmt_type)23532353- continue;23542354- subs = &as->substream[stream];23552355- if (subs->endpoint)23562356- continue;23572357- err = snd_pcm_new_stream(as->pcm, stream, 1);23582358- if (err < 0)23592359- return err;23602360- init_substream(as, stream, fp);23612361- return 0;23622362- }23632363-23642364- /* create a new pcm */23652365- as = kzalloc(sizeof(*as), GFP_KERNEL);23662366- if (!as)23672367- return -ENOMEM;23682368- as->pcm_index = chip->pcm_devs;23692369- as->chip = chip;23702370- as->fmt_type = fp->fmt_type;23712371- err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,23722372- stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,23732373- stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,23742374- &pcm);23752375- if (err < 0) {23762376- kfree(as);23772377- return err;23782378- }23792379- as->pcm = pcm;23802380- pcm->private_data = as;23812381- pcm->private_free = snd_usb_audio_pcm_free;23822382- pcm->info_flags = 0;23832383- if (chip->pcm_devs > 0)23842384- sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);23852385- else23862386- strcpy(pcm->name, "USB Audio");23872387-23882388- init_substream(as, stream, fp);23892389-23902390- list_add(&as->list, &chip->pcm_list);23912391- chip->pcm_devs++;23922392-23932393- proc_pcm_format_add(as);23942394-23952395- return 0;23962396-}23972397-23982398-23992399-/*24002400- * check if the device uses big-endian samples24012401- */24022402-static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)24032403-{24042404- switch (chip->usb_id) {24052405- case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */24062406- if (fp->endpoint & USB_DIR_IN)24072407- return 1;24082408- break;24092409- case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */24102410- if (device_setup[chip->index] == 0x00 ||24112411- fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)24122412- return 1;24132413- }24142414- return 0;24152415-}24162416-24172417-/*24182418- * parse the audio format type I descriptor24192419- * and returns the corresponding pcm format24202420- *24212421- * @dev: usb device24222422- * @fp: audioformat record24232423- * @format: the format tag (wFormatTag)24242424- * @fmt: the format type descriptor24252425- */24262426-static int parse_audio_format_i_type(struct snd_usb_audio *chip,24272427- struct audioformat *fp,24282428- int format, void *_fmt,24292429- int protocol)24302430-{24312431- int pcm_format, i;24322432- int sample_width, sample_bytes;24332433-24342434- switch (protocol) {24352435- case UAC_VERSION_1: {24362436- struct uac_format_type_i_discrete_descriptor *fmt = _fmt;24372437- sample_width = fmt->bBitResolution;24382438- sample_bytes = fmt->bSubframeSize;24392439- break;24402440- }24412441-24422442- case UAC_VERSION_2: {24432443- struct uac_format_type_i_ext_descriptor *fmt = _fmt;24442444- sample_width = fmt->bBitResolution;24452445- sample_bytes = fmt->bSubslotSize;24462446-24472447- /*24482448- * FIXME24492449- * USB audio class v2 devices specify a bitmap of possible24502450- * audio formats rather than one fix value. For now, we just24512451- * pick one of them and report that as the only possible24522452- * value for this setting.24532453- * The bit allocation map is in fact compatible to the24542454- * wFormatTag of the v1 AS streaming descriptors, which is why24552455- * we can simply map the matrix.24562456- */24572457-24582458- for (i = 0; i < 5; i++)24592459- if (format & (1UL << i)) {24602460- format = i + 1;24612461- break;24622462- }24632463-24642464- break;24652465- }24662466-24672467- default:24682468- return -EINVAL;24692469- }24702470-24712471- /* FIXME: correct endianess and sign? */24722472- pcm_format = -1;24732473-24742474- switch (format) {24752475- case UAC_FORMAT_TYPE_I_UNDEFINED: /* some devices don't define this correctly... */24762476- snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",24772477- chip->dev->devnum, fp->iface, fp->altsetting);24782478- /* fall-through */24792479- case UAC_FORMAT_TYPE_I_PCM:24802480- if (sample_width > sample_bytes * 8) {24812481- snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",24822482- chip->dev->devnum, fp->iface, fp->altsetting,24832483- sample_width, sample_bytes);24842484- }24852485- /* check the format byte size */24862486- switch (sample_bytes) {24872487- case 1:24882488- pcm_format = SNDRV_PCM_FORMAT_S8;24892489- break;24902490- case 2:24912491- if (is_big_endian_format(chip, fp))24922492- pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */24932493- else24942494- pcm_format = SNDRV_PCM_FORMAT_S16_LE;24952495- break;24962496- case 3:24972497- if (is_big_endian_format(chip, fp))24982498- pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */24992499- else25002500- pcm_format = SNDRV_PCM_FORMAT_S24_3LE;25012501- break;25022502- case 4:25032503- pcm_format = SNDRV_PCM_FORMAT_S32_LE;25042504- break;25052505- default:25062506- snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",25072507- chip->dev->devnum, fp->iface, fp->altsetting,25082508- sample_width, sample_bytes);25092509- break;25102510- }25112511- break;25122512- case UAC_FORMAT_TYPE_I_PCM8:25132513- pcm_format = SNDRV_PCM_FORMAT_U8;25142514-25152515- /* Dallas DS4201 workaround: it advertises U8 format, but really25162516- supports S8. */25172517- if (chip->usb_id == USB_ID(0x04fa, 0x4201))25182518- pcm_format = SNDRV_PCM_FORMAT_S8;25192519- break;25202520- case UAC_FORMAT_TYPE_I_IEEE_FLOAT:25212521- pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;25222522- break;25232523- case UAC_FORMAT_TYPE_I_ALAW:25242524- pcm_format = SNDRV_PCM_FORMAT_A_LAW;25252525- break;25262526- case UAC_FORMAT_TYPE_I_MULAW:25272527- pcm_format = SNDRV_PCM_FORMAT_MU_LAW;25282528- break;25292529- default:25302530- snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",25312531- chip->dev->devnum, fp->iface, fp->altsetting, format);25322532- break;25332533- }25342534- return pcm_format;25352535-}25362536-25372537-25382538-/*25392539- * parse the format descriptor and stores the possible sample rates25402540- * on the audioformat table (audio class v1).25412541- *25422542- * @dev: usb device25432543- * @fp: audioformat record25442544- * @fmt: the format descriptor25452545- * @offset: the start offset of descriptor pointing the rate type25462546- * (7 for type I and II, 8 for type II)25472547- */25482548-static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,25492549- unsigned char *fmt, int offset)25502550-{25512551- int nr_rates = fmt[offset];25522552-25532553- if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {25542554- snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",25552555- chip->dev->devnum, fp->iface, fp->altsetting);25562556- return -1;25572557- }25582558-25592559- if (nr_rates) {25602560- /*25612561- * build the rate table and bitmap flags25622562- */25632563- int r, idx;25642564-25652565- fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);25662566- if (fp->rate_table == NULL) {25672567- snd_printk(KERN_ERR "cannot malloc\n");25682568- return -1;25692569- }25702570-25712571- fp->nr_rates = 0;25722572- fp->rate_min = fp->rate_max = 0;25732573- for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {25742574- unsigned int rate = combine_triple(&fmt[idx]);25752575- if (!rate)25762576- continue;25772577- /* C-Media CM6501 mislabels its 96 kHz altsetting */25782578- if (rate == 48000 && nr_rates == 1 &&25792579- (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||25802580- chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&25812581- fp->altsetting == 5 && fp->maxpacksize == 392)25822582- rate = 96000;25832583- /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */25842584- if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))25852585- rate = 8000;25862586- fp->rate_table[fp->nr_rates] = rate;25872587- if (!fp->rate_min || rate < fp->rate_min)25882588- fp->rate_min = rate;25892589- if (!fp->rate_max || rate > fp->rate_max)25902590- fp->rate_max = rate;25912591- fp->rates |= snd_pcm_rate_to_rate_bit(rate);25922592- fp->nr_rates++;25932593- }25942594- if (!fp->nr_rates) {25952595- hwc_debug("All rates were zero. Skipping format!\n");25962596- return -1;25972597- }25982598- } else {25992599- /* continuous rates */26002600- fp->rates = SNDRV_PCM_RATE_CONTINUOUS;26012601- fp->rate_min = combine_triple(&fmt[offset + 1]);26022602- fp->rate_max = combine_triple(&fmt[offset + 4]);26032603- }26042604- return 0;26052605-}26062606-26072607-/*26082608- * parse the format descriptor and stores the possible sample rates26092609- * on the audioformat table (audio class v2).26102610- */26112611-static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,26122612- struct audioformat *fp,26132613- struct usb_host_interface *iface)26142614-{26152615- struct usb_device *dev = chip->dev;26162616- unsigned char tmp[2], *data;26172617- int i, nr_rates, data_size, ret = 0;26182618-26192619- /* get the number of sample rates first by only fetching 2 bytes */26202620- ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,26212621- USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,26222622- 0x0100, chip->clock_id << 8, tmp, sizeof(tmp), 1000);26232623-26242624- if (ret < 0) {26252625- snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");26262626- goto err;26272627- }26282628-26292629- nr_rates = (tmp[1] << 8) | tmp[0];26302630- data_size = 2 + 12 * nr_rates;26312631- data = kzalloc(data_size, GFP_KERNEL);26322632- if (!data) {26332633- ret = -ENOMEM;26342634- goto err;26352635- }26362636-26372637- /* now get the full information */26382638- ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,26392639- USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,26402640- 0x0100, chip->clock_id << 8, data, data_size, 1000);26412641-26422642- if (ret < 0) {26432643- snd_printk(KERN_ERR "unable to retrieve sample rate range\n");26442644- ret = -EINVAL;26452645- goto err_free;26462646- }26472647-26482648- fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);26492649- if (!fp->rate_table) {26502650- ret = -ENOMEM;26512651- goto err_free;26522652- }26532653-26542654- fp->nr_rates = 0;26552655- fp->rate_min = fp->rate_max = 0;26562656-26572657- for (i = 0; i < nr_rates; i++) {26582658- int rate = combine_quad(&data[2 + 12 * i]);26592659-26602660- fp->rate_table[fp->nr_rates] = rate;26612661- if (!fp->rate_min || rate < fp->rate_min)26622662- fp->rate_min = rate;26632663- if (!fp->rate_max || rate > fp->rate_max)26642664- fp->rate_max = rate;26652665- fp->rates |= snd_pcm_rate_to_rate_bit(rate);26662666- fp->nr_rates++;26672667- }26682668-26692669-err_free:26702670- kfree(data);26712671-err:26722672- return ret;26732673-}26742674-26752675-/*26762676- * parse the format type I and III descriptors26772677- */26782678-static int parse_audio_format_i(struct snd_usb_audio *chip,26792679- struct audioformat *fp,26802680- int format, void *_fmt,26812681- struct usb_host_interface *iface)26822682-{26832683- struct usb_interface_descriptor *altsd = get_iface_desc(iface);26842684- struct uac_format_type_i_discrete_descriptor *fmt = _fmt;26852685- int protocol = altsd->bInterfaceProtocol;26862686- int pcm_format, ret;26872687-26882688- if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {26892689- /* FIXME: the format type is really IECxxx26902690- * but we give normal PCM format to get the existing26912691- * apps working...26922692- */26932693- switch (chip->usb_id) {26942694-26952695- case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */26962696- if (device_setup[chip->index] == 0x00 && 26972697- fp->altsetting == 6)26982698- pcm_format = SNDRV_PCM_FORMAT_S16_BE;26992699- else27002700- pcm_format = SNDRV_PCM_FORMAT_S16_LE;27012701- break;27022702- default:27032703- pcm_format = SNDRV_PCM_FORMAT_S16_LE;27042704- }27052705- } else {27062706- pcm_format = parse_audio_format_i_type(chip, fp, format, fmt, protocol);27072707- if (pcm_format < 0)27082708- return -1;27092709- }27102710-27112711- fp->format = pcm_format;27122712-27132713- /* gather possible sample rates */27142714- /* audio class v1 reports possible sample rates as part of the27152715- * proprietary class specific descriptor.27162716- * audio class v2 uses class specific EP0 range requests for that.27172717- */27182718- switch (protocol) {27192719- case UAC_VERSION_1:27202720- fp->channels = fmt->bNrChannels;27212721- ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);27222722- break;27232723- case UAC_VERSION_2:27242724- /* fp->channels is already set in this case */27252725- ret = parse_audio_format_rates_v2(chip, fp, iface);27262726- break;27272727- }27282728-27292729- if (fp->channels < 1) {27302730- snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",27312731- chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);27322732- return -1;27332733- }27342734-27352735- return ret;27362736-}27372737-27382738-/*27392739- * parse the format type II descriptor27402740- */27412741-static int parse_audio_format_ii(struct snd_usb_audio *chip,27422742- struct audioformat *fp,27432743- int format, void *_fmt,27442744- struct usb_host_interface *iface)27452745-{27462746- int brate, framesize, ret;27472747- struct usb_interface_descriptor *altsd = get_iface_desc(iface);27482748- int protocol = altsd->bInterfaceProtocol;27492749-27502750- switch (format) {27512751- case UAC_FORMAT_TYPE_II_AC3:27522752- /* FIXME: there is no AC3 format defined yet */27532753- // fp->format = SNDRV_PCM_FORMAT_AC3;27542754- fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */27552755- break;27562756- case UAC_FORMAT_TYPE_II_MPEG:27572757- fp->format = SNDRV_PCM_FORMAT_MPEG;27582758- break;27592759- default:27602760- snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",27612761- chip->dev->devnum, fp->iface, fp->altsetting, format);27622762- fp->format = SNDRV_PCM_FORMAT_MPEG;27632763- break;27642764- }27652765-27662766- fp->channels = 1;27672767-27682768- switch (protocol) {27692769- case UAC_VERSION_1: {27702770- struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;27712771- brate = le16_to_cpu(fmt->wMaxBitRate);27722772- framesize = le16_to_cpu(fmt->wSamplesPerFrame);27732773- snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);27742774- fp->frame_size = framesize;27752775- ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */27762776- break;27772777- }27782778- case UAC_VERSION_2: {27792779- struct uac_format_type_ii_ext_descriptor *fmt = _fmt;27802780- brate = le16_to_cpu(fmt->wMaxBitRate);27812781- framesize = le16_to_cpu(fmt->wSamplesPerFrame);27822782- snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);27832783- fp->frame_size = framesize;27842784- ret = parse_audio_format_rates_v2(chip, fp, iface);27852785- break;27862786- }27872787- }27882788-27892789- return ret;27902790-}27912791-27922792-static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,27932793- int format, unsigned char *fmt, int stream,27942794- struct usb_host_interface *iface)27952795-{27962796- int err;27972797-27982798- switch (fmt[3]) {27992799- case UAC_FORMAT_TYPE_I:28002800- case UAC_FORMAT_TYPE_III:28012801- err = parse_audio_format_i(chip, fp, format, fmt, iface);28022802- break;28032803- case UAC_FORMAT_TYPE_II:28042804- err = parse_audio_format_ii(chip, fp, format, fmt, iface);28052805- break;28062806- default:28072807- snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",28082808- chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);28092809- return -1;28102810- }28112811- fp->fmt_type = fmt[3];28122812- if (err < 0)28132813- return err;28142814-#if 128152815- /* FIXME: temporary hack for extigy/audigy 2 nx/zs */28162816- /* extigy apparently supports sample rates other than 48k28172817- * but not in ordinary way. so we enable only 48k atm.28182818- */28192819- if (chip->usb_id == USB_ID(0x041e, 0x3000) ||28202820- chip->usb_id == USB_ID(0x041e, 0x3020) ||28212821- chip->usb_id == USB_ID(0x041e, 0x3061)) {28222822- if (fmt[3] == UAC_FORMAT_TYPE_I &&28232823- fp->rates != SNDRV_PCM_RATE_48000 &&28242824- fp->rates != SNDRV_PCM_RATE_96000)28252825- return -1;28262826- }28272827-#endif28282828- return 0;28292829-}28302830-28312831-static unsigned char parse_datainterval(struct snd_usb_audio *chip,28322832- struct usb_host_interface *alts)28332833-{28342834- if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&28352835- get_endpoint(alts, 0)->bInterval >= 1 &&28362836- get_endpoint(alts, 0)->bInterval <= 4)28372837- return get_endpoint(alts, 0)->bInterval - 1;28382838- else28392839- return 0;28402840-}28412841-28422842-static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,28432843- int iface, int altno);28442844-static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)28452845-{28462846- struct usb_device *dev;28472847- struct usb_interface *iface;28482848- struct usb_host_interface *alts;28492849- struct usb_interface_descriptor *altsd;28502850- int i, altno, err, stream;28512851- int format = 0, num_channels = 0;28522852- struct audioformat *fp = NULL;28532853- unsigned char *fmt, *csep;28542854- int num, protocol;28552855-28562856- dev = chip->dev;28572857-28582858- /* parse the interface's altsettings */28592859- iface = usb_ifnum_to_if(dev, iface_no);28602860-28612861- num = iface->num_altsetting;28622862-28632863- /*28642864- * Dallas DS4201 workaround: It presents 5 altsettings, but the last28652865- * one misses syncpipe, and does not produce any sound.28662866- */28672867- if (chip->usb_id == USB_ID(0x04fa, 0x4201))28682868- num = 4;28692869-28702870- for (i = 0; i < num; i++) {28712871- alts = &iface->altsetting[i];28722872- altsd = get_iface_desc(alts);28732873- protocol = altsd->bInterfaceProtocol;28742874- /* skip invalid one */28752875- if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&28762876- altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||28772877- (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&28782878- altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||28792879- altsd->bNumEndpoints < 1 ||28802880- le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)28812881- continue;28822882- /* must be isochronous */28832883- if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=28842884- USB_ENDPOINT_XFER_ISOC)28852885- continue;28862886- /* check direction */28872887- stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?28882888- SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;28892889- altno = altsd->bAlternateSetting;28902890-28912891- /* audiophile usb: skip altsets incompatible with device_setup28922892- */28932893- if (chip->usb_id == USB_ID(0x0763, 0x2003) && 28942894- audiophile_skip_setting_quirk(chip, iface_no, altno))28952895- continue;28962896-28972897- /* get audio formats */28982898- switch (protocol) {28992899- case UAC_VERSION_1: {29002900- struct uac_as_header_descriptor_v1 *as =29012901- snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);29022902-29032903- if (!as) {29042904- snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",29052905- dev->devnum, iface_no, altno);29062906- continue;29072907- }29082908-29092909- if (as->bLength < sizeof(*as)) {29102910- snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",29112911- dev->devnum, iface_no, altno);29122912- continue;29132913- }29142914-29152915- format = le16_to_cpu(as->wFormatTag); /* remember the format value */29162916- break;29172917- }29182918-29192919- case UAC_VERSION_2: {29202920- struct uac_as_header_descriptor_v2 *as =29212921- snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);29222922-29232923- if (!as) {29242924- snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",29252925- dev->devnum, iface_no, altno);29262926- continue;29272927- }29282928-29292929- if (as->bLength < sizeof(*as)) {29302930- snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",29312931- dev->devnum, iface_no, altno);29322932- continue;29332933- }29342934-29352935- num_channels = as->bNrChannels;29362936- format = le32_to_cpu(as->bmFormats);29372937-29382938- break;29392939- }29402940-29412941- default:29422942- snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",29432943- dev->devnum, iface_no, altno, protocol);29442944- continue;29452945- }29462946-29472947- /* get format type */29482948- fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);29492949- if (!fmt) {29502950- snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",29512951- dev->devnum, iface_no, altno);29522952- continue;29532953- }29542954- if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||29552955- ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {29562956- snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",29572957- dev->devnum, iface_no, altno);29582958- continue;29592959- }29602960-29612961- /*29622962- * Blue Microphones workaround: The last altsetting is identical29632963- * with the previous one, except for a larger packet size, but29642964- * is actually a mislabeled two-channel setting; ignore it.29652965- */29662966- if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&29672967- fp && fp->altsetting == 1 && fp->channels == 1 &&29682968- fp->format == SNDRV_PCM_FORMAT_S16_LE &&29692969- protocol == UAC_VERSION_1 &&29702970- le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==29712971- fp->maxpacksize * 2)29722972- continue;29732973-29742974- csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);29752975- /* Creamware Noah has this descriptor after the 2nd endpoint */29762976- if (!csep && altsd->bNumEndpoints >= 2)29772977- csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);29782978- if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {29792979- snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"29802980- " class specific endpoint descriptor\n",29812981- dev->devnum, iface_no, altno);29822982- csep = NULL;29832983- }29842984-29852985- fp = kzalloc(sizeof(*fp), GFP_KERNEL);29862986- if (! fp) {29872987- snd_printk(KERN_ERR "cannot malloc\n");29882988- return -ENOMEM;29892989- }29902990-29912991- fp->iface = iface_no;29922992- fp->altsetting = altno;29932993- fp->altset_idx = i;29942994- fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;29952995- fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;29962996- fp->datainterval = parse_datainterval(chip, alts);29972997- fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);29982998- /* num_channels is only set for v2 interfaces */29992999- fp->channels = num_channels;30003000- if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)30013001- fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)30023002- * (fp->maxpacksize & 0x7ff);30033003- fp->attributes = csep ? csep[3] : 0;30043004-30053005- /* some quirks for attributes here */30063006-30073007- switch (chip->usb_id) {30083008- case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */30093009- /* Optoplay sets the sample rate attribute although30103010- * it seems not supporting it in fact.30113011- */30123012- fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;30133013- break;30143014- case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */30153015- case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */30163016- /* doesn't set the sample rate attribute, but supports it */30173017- fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;30183018- break;30193019- case USB_ID(0x047f, 0x0ca1): /* plantronics headset */30203020- case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is30213021- an older model 77d:223) */30223022- /*30233023- * plantronics headset and Griffin iMic have set adaptive-in30243024- * although it's really not...30253025- */30263026- fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;30273027- if (stream == SNDRV_PCM_STREAM_PLAYBACK)30283028- fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;30293029- else30303030- fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;30313031- break;30323032- }30333033-30343034- /* ok, let's parse further... */30353035- if (parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {30363036- kfree(fp->rate_table);30373037- kfree(fp);30383038- continue;30393039- }30403040-30413041- snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);30423042- err = add_audio_endpoint(chip, stream, fp);30433043- if (err < 0) {30443044- kfree(fp->rate_table);30453045- kfree(fp);30463046- return err;30473047- }30483048- /* try to set the interface... */30493049- usb_set_interface(chip->dev, iface_no, altno);30503050- init_usb_pitch(chip->dev, iface_no, alts, fp);30513051- init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);30523052- }30533053- return 0;30543054-}30553055-30563056-30573057-/*30583058- * disconnect streams30593059- * called from snd_usb_audio_disconnect()30603060- */30613061-static void snd_usb_stream_disconnect(struct list_head *head)30623062-{30633063- int idx;30643064- struct snd_usb_stream *as;30653065- struct snd_usb_substream *subs;30663066-30673067- as = list_entry(head, struct snd_usb_stream, list);30683068- for (idx = 0; idx < 2; idx++) {30693069- subs = &as->substream[idx];30703070- if (!subs->num_formats)30713071- return;30723072- release_substream_urbs(subs, 1);30733073- subs->interface = -1;30743074- }30753075-}30763076-30773077-static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)30783078-{30793079- struct usb_device *dev = chip->dev;30803080- struct usb_host_interface *alts;30813081- struct usb_interface_descriptor *altsd;30823082- struct usb_interface *iface = usb_ifnum_to_if(dev, interface);30833083-30843084- if (!iface) {30853085- snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",30863086- dev->devnum, ctrlif, interface);30873087- return -EINVAL;30883088- }30893089-30903090- if (usb_interface_claimed(iface)) {30913091- snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",30923092- dev->devnum, ctrlif, interface);30933093- return -EINVAL;30943094- }30953095-30963096- alts = &iface->altsetting[0];30973097- altsd = get_iface_desc(alts);30983098- if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||30993099- altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&31003100- altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {31013101- int err = snd_usbmidi_create(chip->card, iface,31023102- &chip->midi_list, NULL);31033103- if (err < 0) {31043104- snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",31053105- dev->devnum, ctrlif, interface);31063106- return -EINVAL;31073107- }31083108- usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);31093109-31103110- return 0;31113111- }31123112-31133113- if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&31143114- altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||31153115- altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {31163116- snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",31173117- dev->devnum, ctrlif, interface, altsd->bInterfaceClass);31183118- /* skip non-supported classes */31193119- return -EINVAL;31203120- }31213121-31223122- if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {31233123- snd_printk(KERN_ERR "low speed audio streaming not supported\n");31243124- return -EINVAL;31253125- }31263126-31273127- if (! parse_audio_endpoints(chip, interface)) {31283128- usb_set_interface(dev, interface, 0); /* reset the current interface */31293129- usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);31303130- return -EINVAL;31313131- }31323132-31333133- return 0;31343134-}31353135-31363136-/*31373137- * parse audio control descriptor and create pcm/midi streams31383138- */31393139-static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)31403140-{31413141- struct usb_device *dev = chip->dev;31423142- struct usb_host_interface *host_iface;31433143- struct usb_interface_descriptor *altsd;31443144- void *control_header;31453145- int i, protocol;31463146-31473147- /* find audiocontrol interface */31483148- host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];31493149- control_header = snd_usb_find_csint_desc(host_iface->extra,31503150- host_iface->extralen,31513151- NULL, UAC_HEADER);31523152- altsd = get_iface_desc(host_iface);31533153- protocol = altsd->bInterfaceProtocol;31543154-31553155- if (!control_header) {31563156- snd_printk(KERN_ERR "cannot find UAC_HEADER\n");31573157- return -EINVAL;31583158- }31593159-31603160- switch (protocol) {31613161- case UAC_VERSION_1: {31623162- struct uac_ac_header_descriptor_v1 *h1 = control_header;31633163-31643164- if (!h1->bInCollection) {31653165- snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");31663166- return -EINVAL;31673167- }31683168-31693169- if (h1->bLength < sizeof(*h1) + h1->bInCollection) {31703170- snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");31713171- return -EINVAL;31723172- }31733173-31743174- for (i = 0; i < h1->bInCollection; i++)31753175- snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);31763176-31773177- break;31783178- }31793179-31803180- case UAC_VERSION_2: {31813181- struct uac_clock_source_descriptor *cs;31823182- struct usb_interface_assoc_descriptor *assoc =31833183- usb_ifnum_to_if(dev, ctrlif)->intf_assoc;31843184-31853185- if (!assoc) {31863186- snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");31873187- return -EINVAL;31883188- }31893189-31903190- /* FIXME: for now, we expect there is at least one clock source31913191- * descriptor and we always take the first one.31923192- * We should properly support devices with multiple clock sources,31933193- * clock selectors and sample rate conversion units. */31943194-31953195- cs = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen,31963196- NULL, UAC_CLOCK_SOURCE);31973197-31983198- if (!cs) {31993199- snd_printk(KERN_ERR "CLOCK_SOURCE descriptor not found\n");32003200- return -EINVAL;32013201- }32023202-32033203- chip->clock_id = cs->bClockID;32043204-32053205- for (i = 0; i < assoc->bInterfaceCount; i++) {32063206- int intf = assoc->bFirstInterface + i;32073207-32083208- if (intf != ctrlif)32093209- snd_usb_create_stream(chip, ctrlif, intf);32103210- }32113211-32123212- break;32133213- }32143214-32153215- default:32163216- snd_printk(KERN_ERR "unknown protocol version 0x%02x\n", protocol);32173217- return -EINVAL;32183218- }32193219-32203220- return 0;32213221-}32223222-32233223-/*32243224- * create a stream for an endpoint/altsetting without proper descriptors32253225- */32263226-static int create_fixed_stream_quirk(struct snd_usb_audio *chip,32273227- struct usb_interface *iface,32283228- const struct snd_usb_audio_quirk *quirk)32293229-{32303230- struct audioformat *fp;32313231- struct usb_host_interface *alts;32323232- int stream, err;32333233- unsigned *rate_table = NULL;32343234-32353235- fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);32363236- if (! fp) {32373237- snd_printk(KERN_ERR "cannot memdup\n");32383238- return -ENOMEM;32393239- }32403240- if (fp->nr_rates > 0) {32413241- rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);32423242- if (!rate_table) {32433243- kfree(fp);32443244- return -ENOMEM;32453245- }32463246- memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);32473247- fp->rate_table = rate_table;32483248- }32493249-32503250- stream = (fp->endpoint & USB_DIR_IN)32513251- ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;32523252- err = add_audio_endpoint(chip, stream, fp);32533253- if (err < 0) {32543254- kfree(fp);32553255- kfree(rate_table);32563256- return err;32573257- }32583258- if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||32593259- fp->altset_idx >= iface->num_altsetting) {32603260- kfree(fp);32613261- kfree(rate_table);32623262- return -EINVAL;32633263- }32643264- alts = &iface->altsetting[fp->altset_idx];32653265- fp->datainterval = parse_datainterval(chip, alts);32663266- fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);32673267- usb_set_interface(chip->dev, fp->iface, 0);32683268- init_usb_pitch(chip->dev, fp->iface, alts, fp);32693269- init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);32703270- return 0;32713271-}32723272-32733273-/*32743274- * create a stream for an interface with proper descriptors32753275- */32763276-static int create_standard_audio_quirk(struct snd_usb_audio *chip,32773277- struct usb_interface *iface,32783278- const struct snd_usb_audio_quirk *quirk)32793279-{32803280- struct usb_host_interface *alts;32813281- struct usb_interface_descriptor *altsd;32823282- int err;32833283-32843284- alts = &iface->altsetting[0];32853285- altsd = get_iface_desc(alts);32863286- err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);32873287- if (err < 0) {32883288- snd_printk(KERN_ERR "cannot setup if %d: error %d\n",32893289- altsd->bInterfaceNumber, err);32903290- return err;32913291- }32923292- /* reset the current interface */32933293- usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);32943294- return 0;32953295-}32963296-32973297-/*32983298- * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface. 32993299- * The only way to detect the sample rate is by looking at wMaxPacketSize.33003300- */33013301-static int create_uaxx_quirk(struct snd_usb_audio *chip,33023302- struct usb_interface *iface,33033303- const struct snd_usb_audio_quirk *quirk)33043304-{33053305- static const struct audioformat ua_format = {33063306- .format = SNDRV_PCM_FORMAT_S24_3LE,33073307- .channels = 2,33083308- .fmt_type = UAC_FORMAT_TYPE_I,33093309- .altsetting = 1,33103310- .altset_idx = 1,33113311- .rates = SNDRV_PCM_RATE_CONTINUOUS,33123312- };33133313- struct usb_host_interface *alts;33143314- struct usb_interface_descriptor *altsd;33153315- struct audioformat *fp;33163316- int stream, err;33173317-33183318- /* both PCM and MIDI interfaces have 2 or more altsettings */33193319- if (iface->num_altsetting < 2)33203320- return -ENXIO;33213321- alts = &iface->altsetting[1];33223322- altsd = get_iface_desc(alts);33233323-33243324- if (altsd->bNumEndpoints == 2) {33253325- static const struct snd_usb_midi_endpoint_info ua700_ep = {33263326- .out_cables = 0x0003,33273327- .in_cables = 0x000333283328- };33293329- static const struct snd_usb_audio_quirk ua700_quirk = {33303330- .type = QUIRK_MIDI_FIXED_ENDPOINT,33313331- .data = &ua700_ep33323332- };33333333- static const struct snd_usb_midi_endpoint_info uaxx_ep = {33343334- .out_cables = 0x0001,33353335- .in_cables = 0x000133363336- };33373337- static const struct snd_usb_audio_quirk uaxx_quirk = {33383338- .type = QUIRK_MIDI_FIXED_ENDPOINT,33393339- .data = &uaxx_ep33403340- };33413341- const struct snd_usb_audio_quirk *quirk =33423342- chip->usb_id == USB_ID(0x0582, 0x002b)33433343- ? &ua700_quirk : &uaxx_quirk;33443344- return snd_usbmidi_create(chip->card, iface,33453345- &chip->midi_list, quirk);33463346- }33473347-33483348- if (altsd->bNumEndpoints != 1)33493349- return -ENXIO;33503350-33513351- fp = kmalloc(sizeof(*fp), GFP_KERNEL);33523352- if (!fp)33533353- return -ENOMEM;33543354- memcpy(fp, &ua_format, sizeof(*fp));33553355-33563356- fp->iface = altsd->bInterfaceNumber;33573357- fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;33583358- fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;33593359- fp->datainterval = 0;33603360- fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);33613361-33623362- switch (fp->maxpacksize) {33633363- case 0x120:33643364- fp->rate_max = fp->rate_min = 44100;33653365- break;33663366- case 0x138:33673367- case 0x140:33683368- fp->rate_max = fp->rate_min = 48000;33693369- break;33703370- case 0x258:33713371- case 0x260:33723372- fp->rate_max = fp->rate_min = 96000;33733373- break;33743374- default:33753375- snd_printk(KERN_ERR "unknown sample rate\n");33763376- kfree(fp);33773377- return -ENXIO;33783378- }33793379-33803380- stream = (fp->endpoint & USB_DIR_IN)33813381- ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;33823382- err = add_audio_endpoint(chip, stream, fp);33833383- if (err < 0) {33843384- kfree(fp);33853385- return err;33863386- }33873387- usb_set_interface(chip->dev, fp->iface, 0);33883388- return 0;33893389-}33903390-33913391-static int snd_usb_create_quirk(struct snd_usb_audio *chip,33923392- struct usb_interface *iface,33933393- const struct snd_usb_audio_quirk *quirk);33943394-33953395-/*33963396- * handle the quirks for the contained interfaces33973397- */33983398-static int create_composite_quirk(struct snd_usb_audio *chip,33993399- struct usb_interface *iface,34003400- const struct snd_usb_audio_quirk *quirk)34013401-{34023402- int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;34033403- int err;34043404-34053405- for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {34063406- iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);34073407- if (!iface)34083408- continue;34093409- if (quirk->ifnum != probed_ifnum &&34103410- usb_interface_claimed(iface))34113411- continue;34123412- err = snd_usb_create_quirk(chip, iface, quirk);34133413- if (err < 0)34143414- return err;34153415- if (quirk->ifnum != probed_ifnum)34163416- usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);34173417- }34183418- return 0;34193419-}34203420-34213421-static int ignore_interface_quirk(struct snd_usb_audio *chip,34223422- struct usb_interface *iface,34233423- const struct snd_usb_audio_quirk *quirk)34243424-{34253425- return 0;34263426-}34273427-34283428-/*34293429- * Allow alignment on audio sub-slot (channel samples) rather than34303430- * on audio slots (audio frames)34313431- */34323432-static int create_align_transfer_quirk(struct snd_usb_audio *chip,34333433- struct usb_interface *iface,34343434- const struct snd_usb_audio_quirk *quirk)34353435-{34363436- chip->txfr_quirk = 1;34373437- return 1; /* Continue with creating streams and mixer */34383438-}34393439-34403440-34413441-/*34423442- * boot quirks34433443- */34443444-34453445-#define EXTIGY_FIRMWARE_SIZE_OLD 79434463446-#define EXTIGY_FIRMWARE_SIZE_NEW 48334473447-34483448-static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)34493449-{34503450- struct usb_host_config *config = dev->actconfig;34513451- int err;34523452-34533453- if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||34543454- le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {34553455- snd_printdd("sending Extigy boot sequence...\n");34563456- /* Send message to force it to reconnect with full interface. */34573457- err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),34583458- 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);34593459- if (err < 0) snd_printdd("error sending boot message: %d\n", err);34603460- err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,34613461- &dev->descriptor, sizeof(dev->descriptor));34623462- config = dev->actconfig;34633463- if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);34643464- err = usb_reset_configuration(dev);34653465- if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);34663466- snd_printdd("extigy_boot: new boot length = %d\n",34673467- le16_to_cpu(get_cfg_desc(config)->wTotalLength));34683468- return -ENODEV; /* quit this anyway */34693469- }34703470- return 0;34713471-}34723472-34733473-static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)34743474-{34753475- u8 buf = 1;34763476-34773477- snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,34783478- USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,34793479- 0, 0, &buf, 1, 1000);34803480- if (buf == 0) {34813481- snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,34823482- USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,34833483- 1, 2000, NULL, 0, 1000);34843484- return -ENODEV;34853485- }34863486- return 0;34873487-}34883488-34893489-/*34903490- * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely34913491- * documented in the device's data sheet.34923492- */34933493-static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)34943494-{34953495- u8 buf[4];34963496- buf[0] = 0x20;34973497- buf[1] = value & 0xff;34983498- buf[2] = (value >> 8) & 0xff;34993499- buf[3] = reg;35003500- return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,35013501- USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,35023502- 0, 0, &buf, 4, 1000);35033503-}35043504-35053505-static int snd_usb_cm106_boot_quirk(struct usb_device *dev)35063506-{35073507- /*35083508- * Enable line-out driver mode, set headphone source to front35093509- * channels, enable stereo mic.35103510- */35113511- return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);35123512-}35133513-35143514-/*35153515- * C-Media CM6206 is based on CM106 with two additional35163516- * registers that are not documented in the data sheet.35173517- * Values here are chosen based on sniffing USB traffic35183518- * under Windows.35193519- */35203520-static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)35213521-{35223522- int err, reg;35233523- int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};35243524-35253525- for (reg = 0; reg < ARRAY_SIZE(val); reg++) {35263526- err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);35273527- if (err < 0)35283528- return err;35293529- }35303530-35313531- return err;35323532-}35333533-35343534-/*35353535- * This call will put the synth in "USB send" mode, i.e it will send MIDI35363536- * messages through USB (this is disabled at startup). The synth will35373537- * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB35383538- * sign on its LCD. Values here are chosen based on sniffing USB traffic35393539- * under Windows.35403540- */35413541-static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)35423542-{35433543- int err, actual_length;35443544-35453545- /* "midi send" enable */35463546- static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };35473547-35483548- void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);35493549- if (!buf)35503550- return -ENOMEM;35513551- err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,35523552- ARRAY_SIZE(seq), &actual_length, 1000);35533553- kfree(buf);35543554- if (err < 0)35553555- return err;35563556-35573557- return 0;35583558-}35593559-35603560-/*35613561- * Setup quirks35623562- */35633563-#define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */35643564-#define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */35653565-#define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */35663566-#define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */35673567-#define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */35683568-#define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */35693569-#define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */35703570-#define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */35713571-#define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */35723572-#define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */35733573-35743574-static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,35753575- int iface, int altno)35763576-{35773577- /* Reset ALL ifaces to 0 altsetting.35783578- * Call it for every possible altsetting of every interface.35793579- */35803580- usb_set_interface(chip->dev, iface, 0);35813581-35823582- if (device_setup[chip->index] & AUDIOPHILE_SET) {35833583- if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS)35843584- && altno != 6)35853585- return 1; /* skip this altsetting */35863586- if ((device_setup[chip->index] & AUDIOPHILE_SET_96K)35873587- && altno != 1)35883588- return 1; /* skip this altsetting */35893589- if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==35903590- AUDIOPHILE_SET_24B_48K_DI && altno != 2)35913591- return 1; /* skip this altsetting */35923592- if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==35933593- AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)35943594- return 1; /* skip this altsetting */35953595- if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==35963596- AUDIOPHILE_SET_16B_48K_DI && altno != 4)35973597- return 1; /* skip this altsetting */35983598- if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==35993599- AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)36003600- return 1; /* skip this altsetting */36013601- } 36023602- return 0; /* keep this altsetting */36033603-}36043604-36053605-static int create_any_midi_quirk(struct snd_usb_audio *chip,36063606- struct usb_interface *intf,36073607- const struct snd_usb_audio_quirk *quirk)36083608-{36093609- return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);36103610-}36113611-36123612-/*36133613- * audio-interface quirks36143614- *36153615- * returns zero if no standard audio/MIDI parsing is needed.36163616- * returns a postive value if standard audio/midi interfaces are parsed36173617- * after this.36183618- * returns a negative value at error.36193619- */36203620-static int snd_usb_create_quirk(struct snd_usb_audio *chip,36213621- struct usb_interface *iface,36223622- const struct snd_usb_audio_quirk *quirk)36233623-{36243624- typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,36253625- const struct snd_usb_audio_quirk *);36263626- static const quirk_func_t quirk_funcs[] = {36273627- [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,36283628- [QUIRK_COMPOSITE] = create_composite_quirk,36293629- [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,36303630- [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,36313631- [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,36323632- [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,36333633- [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,36343634- [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,36353635- [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,36363636- [QUIRK_MIDI_CME] = create_any_midi_quirk,36373637- [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,36383638- [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,36393639- [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,36403640- [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk36413641- };36423642-36433643- if (quirk->type < QUIRK_TYPE_COUNT) {36443644- return quirk_funcs[quirk->type](chip, iface, quirk);36453645- } else {36463646- snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);36473647- return -ENXIO;36483648- }36493649-}36503650-36513651-36523652-/*36533653- * common proc files to show the usb device info36543654- */36553655-static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)36563656-{36573657- struct snd_usb_audio *chip = entry->private_data;36583658- if (!chip->shutdown)36593659- snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);36603660-}36613661-36623662-static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)36633663-{36643664- struct snd_usb_audio *chip = entry->private_data;36653665- if (!chip->shutdown)36663666- snd_iprintf(buffer, "%04x:%04x\n", 36673667- USB_ID_VENDOR(chip->usb_id),36683668- USB_ID_PRODUCT(chip->usb_id));36693669-}36703670-36713671-static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)36723672-{36733673- struct snd_info_entry *entry;36743674- if (!snd_card_proc_new(chip->card, "usbbus", &entry))36753675- snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);36763676- if (!snd_card_proc_new(chip->card, "usbid", &entry))36773677- snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);36783678-}36793679-36803680-/*36813681- * free the chip instance36823682- *36833683- * here we have to do not much, since pcm and controls are already freed36843684- *36853685- */36863686-36873687-static int snd_usb_audio_free(struct snd_usb_audio *chip)36883688-{36893689- kfree(chip);36903690- return 0;36913691-}36923692-36933693-static int snd_usb_audio_dev_free(struct snd_device *device)36943694-{36953695- struct snd_usb_audio *chip = device->device_data;36963696- return snd_usb_audio_free(chip);36973697-}36983698-36993699-37003700-/*37013701- * create a chip instance and set its names.37023702- */37033703-static int snd_usb_audio_create(struct usb_device *dev, int idx,37043704- const struct snd_usb_audio_quirk *quirk,37053705- struct snd_usb_audio **rchip)37063706-{37073707- struct snd_card *card;37083708- struct snd_usb_audio *chip;37093709- int err, len;37103710- char component[14];37113711- static struct snd_device_ops ops = {37123712- .dev_free = snd_usb_audio_dev_free,37133713- };37143714-37153715- *rchip = NULL;37163716-37173717- if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&37183718- snd_usb_get_speed(dev) != USB_SPEED_FULL &&37193719- snd_usb_get_speed(dev) != USB_SPEED_HIGH) {37203720- snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));37213721- return -ENXIO;37223722- }37233723-37243724- err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);37253725- if (err < 0) {37263726- snd_printk(KERN_ERR "cannot create card instance %d\n", idx);37273727- return err;37283728- }37293729-37303730- chip = kzalloc(sizeof(*chip), GFP_KERNEL);37313731- if (! chip) {37323732- snd_card_free(card);37333733- return -ENOMEM;37343734- }37353735-37363736- chip->index = idx;37373737- chip->dev = dev;37383738- chip->card = card;37393739- chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),37403740- le16_to_cpu(dev->descriptor.idProduct));37413741- INIT_LIST_HEAD(&chip->pcm_list);37423742- INIT_LIST_HEAD(&chip->midi_list);37433743- INIT_LIST_HEAD(&chip->mixer_list);37443744-37453745- if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {37463746- snd_usb_audio_free(chip);37473747- snd_card_free(card);37483748- return err;37493749- }37503750-37513751- strcpy(card->driver, "USB-Audio");37523752- sprintf(component, "USB%04x:%04x",37533753- USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));37543754- snd_component_add(card, component);37553755-37563756- /* retrieve the device string as shortname */37573757- if (quirk && quirk->product_name) {37583758- strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));37593759- } else {37603760- if (!dev->descriptor.iProduct ||37613761- usb_string(dev, dev->descriptor.iProduct,37623762- card->shortname, sizeof(card->shortname)) <= 0) {37633763- /* no name available from anywhere, so use ID */37643764- sprintf(card->shortname, "USB Device %#04x:%#04x",37653765- USB_ID_VENDOR(chip->usb_id),37663766- USB_ID_PRODUCT(chip->usb_id));37673767- }37683768- }37693769-37703770- /* retrieve the vendor and device strings as longname */37713771- if (quirk && quirk->vendor_name) {37723772- len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));37733773- } else {37743774- if (dev->descriptor.iManufacturer)37753775- len = usb_string(dev, dev->descriptor.iManufacturer,37763776- card->longname, sizeof(card->longname));37773777- else37783778- len = 0;37793779- /* we don't really care if there isn't any vendor string */37803780- }37813781- if (len > 0)37823782- strlcat(card->longname, " ", sizeof(card->longname));37833783-37843784- strlcat(card->longname, card->shortname, sizeof(card->longname));37853785-37863786- len = strlcat(card->longname, " at ", sizeof(card->longname));37873787-37883788- if (len < sizeof(card->longname))37893789- usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);37903790-37913791- strlcat(card->longname,37923792- snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :37933793- snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :37943794- ", high speed",37953795- sizeof(card->longname));37963796-37973797- snd_usb_audio_create_proc(chip);37983798-37993799- *rchip = chip;38003800- return 0;38013801-}38023802-38033803-38043804-/*38053805- * probe the active usb device38063806- *38073807- * note that this can be called multiple times per a device, when it38083808- * includes multiple audio control interfaces.38093809- *38103810- * thus we check the usb device pointer and creates the card instance38113811- * only at the first time. the successive calls of this function will38123812- * append the pcm interface to the corresponding card.38133813- */38143814-static void *snd_usb_audio_probe(struct usb_device *dev,38153815- struct usb_interface *intf,38163816- const struct usb_device_id *usb_id)38173817-{38183818- const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;38193819- int i, err;38203820- struct snd_usb_audio *chip;38213821- struct usb_host_interface *alts;38223822- int ifnum;38233823- u32 id;38243824-38253825- alts = &intf->altsetting[0];38263826- ifnum = get_iface_desc(alts)->bInterfaceNumber;38273827- id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),38283828- le16_to_cpu(dev->descriptor.idProduct));38293829- if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)38303830- goto __err_val;38313831-38323832- /* SB Extigy needs special boot-up sequence */38333833- /* if more models come, this will go to the quirk list. */38343834- if (id == USB_ID(0x041e, 0x3000)) {38353835- if (snd_usb_extigy_boot_quirk(dev, intf) < 0)38363836- goto __err_val;38373837- }38383838- /* SB Audigy 2 NX needs its own boot-up magic, too */38393839- if (id == USB_ID(0x041e, 0x3020)) {38403840- if (snd_usb_audigy2nx_boot_quirk(dev) < 0)38413841- goto __err_val;38423842- }38433843-38443844- /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */38453845- if (id == USB_ID(0x10f5, 0x0200)) {38463846- if (snd_usb_cm106_boot_quirk(dev) < 0)38473847- goto __err_val;38483848- }38493849-38503850- /* C-Media CM6206 / CM106-Like Sound Device */38513851- if (id == USB_ID(0x0d8c, 0x0102)) {38523852- if (snd_usb_cm6206_boot_quirk(dev) < 0)38533853- goto __err_val;38543854- }38553855-38563856- /* Access Music VirusTI Desktop */38573857- if (id == USB_ID(0x133e, 0x0815)) {38583858- if (snd_usb_accessmusic_boot_quirk(dev) < 0)38593859- goto __err_val;38603860- }38613861-38623862- /*38633863- * found a config. now register to ALSA38643864- */38653865-38663866- /* check whether it's already registered */38673867- chip = NULL;38683868- mutex_lock(®ister_mutex);38693869- for (i = 0; i < SNDRV_CARDS; i++) {38703870- if (usb_chip[i] && usb_chip[i]->dev == dev) {38713871- if (usb_chip[i]->shutdown) {38723872- snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");38733873- goto __error;38743874- }38753875- chip = usb_chip[i];38763876- break;38773877- }38783878- }38793879- if (! chip) {38803880- /* it's a fresh one.38813881- * now look for an empty slot and create a new card instance38823882- */38833883- for (i = 0; i < SNDRV_CARDS; i++)38843884- if (enable[i] && ! usb_chip[i] &&38853885- (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&38863886- (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {38873887- if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {38883888- goto __error;38893889- }38903890- snd_card_set_dev(chip->card, &intf->dev);38913891- break;38923892- }38933893- if (!chip) {38943894- printk(KERN_ERR "no available usb audio device\n");38953895- goto __error;38963896- }38973897- }38983898-38993899- chip->txfr_quirk = 0;39003900- err = 1; /* continue */39013901- if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {39023902- /* need some special handlings */39033903- if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)39043904- goto __error;39053905- }39063906-39073907- if (err > 0) {39083908- /* create normal USB audio interfaces */39093909- if (snd_usb_create_streams(chip, ifnum) < 0 ||39103910- snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {39113911- goto __error;39123912- }39133913- }39143914-39153915- /* we are allowed to call snd_card_register() many times */39163916- if (snd_card_register(chip->card) < 0) {39173917- goto __error;39183918- }39193919-39203920- usb_chip[chip->index] = chip;39213921- chip->num_interfaces++;39223922- mutex_unlock(®ister_mutex);39233923- return chip;39243924-39253925- __error:39263926- if (chip && !chip->num_interfaces)39273927- snd_card_free(chip->card);39283928- mutex_unlock(®ister_mutex);39293929- __err_val:39303930- return NULL;39313931-}39323932-39333933-/*39343934- * we need to take care of counter, since disconnection can be called also39353935- * many times as well as usb_audio_probe().39363936- */39373937-static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)39383938-{39393939- struct snd_usb_audio *chip;39403940- struct snd_card *card;39413941- struct list_head *p;39423942-39433943- if (ptr == (void *)-1L)39443944- return;39453945-39463946- chip = ptr;39473947- card = chip->card;39483948- mutex_lock(®ister_mutex);39493949- chip->shutdown = 1;39503950- chip->num_interfaces--;39513951- if (chip->num_interfaces <= 0) {39523952- snd_card_disconnect(card);39533953- /* release the pcm resources */39543954- list_for_each(p, &chip->pcm_list) {39553955- snd_usb_stream_disconnect(p);39563956- }39573957- /* release the midi resources */39583958- list_for_each(p, &chip->midi_list) {39593959- snd_usbmidi_disconnect(p);39603960- }39613961- /* release mixer resources */39623962- list_for_each(p, &chip->mixer_list) {39633963- snd_usb_mixer_disconnect(p);39643964- }39653965- usb_chip[chip->index] = NULL;39663966- mutex_unlock(®ister_mutex);39673967- snd_card_free_when_closed(card);39683968- } else {39693969- mutex_unlock(®ister_mutex);39703970- }39713971-}39723972-39733973-/*39743974- * new 2.5 USB kernel API39753975- */39763976-static int usb_audio_probe(struct usb_interface *intf,39773977- const struct usb_device_id *id)39783978-{39793979- void *chip;39803980- chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);39813981- if (chip) {39823982- usb_set_intfdata(intf, chip);39833983- return 0;39843984- } else39853985- return -EIO;39863986-}39873987-39883988-static void usb_audio_disconnect(struct usb_interface *intf)39893989-{39903990- snd_usb_audio_disconnect(interface_to_usbdev(intf),39913991- usb_get_intfdata(intf));39923992-}39933993-39943994-#ifdef CONFIG_PM39953995-static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)39963996-{39973997- struct snd_usb_audio *chip = usb_get_intfdata(intf);39983998- struct list_head *p;39993999- struct snd_usb_stream *as;40004000-40014001- if (chip == (void *)-1L)40024002- return 0;40034003-40044004- snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);40054005- if (!chip->num_suspended_intf++) {40064006- list_for_each(p, &chip->pcm_list) {40074007- as = list_entry(p, struct snd_usb_stream, list);40084008- snd_pcm_suspend_all(as->pcm);40094009- }40104010- }40114011-40124012- return 0;40134013-}40144014-40154015-static int usb_audio_resume(struct usb_interface *intf)40164016-{40174017- struct snd_usb_audio *chip = usb_get_intfdata(intf);40184018-40194019- if (chip == (void *)-1L)40204020- return 0;40214021- if (--chip->num_suspended_intf)40224022- return 0;40234023- /*40244024- * ALSA leaves material resumption to user space40254025- * we just notify40264026- */40274027-40284028- snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);40294029-40304030- return 0;40314031-}40324032-#endif /* CONFIG_PM */40334033-40344034-static int __init snd_usb_audio_init(void)40354035-{40364036- if (nrpacks < 1 || nrpacks > MAX_PACKS) {40374037- printk(KERN_WARNING "invalid nrpacks value.\n");40384038- return -EINVAL;40394039- }40404040- return usb_register(&usb_audio_driver);40414041-}40424042-40434043-40444044-static void __exit snd_usb_audio_cleanup(void)40454045-{40464046- usb_deregister(&usb_audio_driver);40474047-}40484048-40494049-module_init(snd_usb_audio_init);40504050-module_exit(snd_usb_audio_cleanup);
+5-88
sound/usb/usbaudio.h
···2121 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA2222 */23232424-/* maximum number of endpoints per interface */2525-#define MIDI_MAX_ENDPOINTS 22626-2724/* handling of USB vendor/product ID pairs as 32-bit numbers */2825#define USB_ID(vendor, product) (((vendor) << 16) | (product))2926#define USB_ID_VENDOR(id) ((id) >> 16)3027#define USB_ID_PRODUCT(id) ((u16)(id))31283229/*3030+ *3331 */34323533struct snd_usb_audio {···4951 struct list_head midi_list; /* list of midi interfaces */50525153 struct list_head mixer_list; /* list of mixer interfaces */5454+5555+ int setup; /* from the 'device_setup' module param */5656+ int nrpacks; /* from the 'nrpacks' module param */5757+ int async_unlink; /* from the 'async_unlink' module param */5258};53595460/*···9189 const void *data;9290};93919494-/* data for QUIRK_MIDI_FIXED_ENDPOINT */9595-struct snd_usb_midi_endpoint_info {9696- int8_t out_ep; /* ep number, 0 autodetect */9797- uint8_t out_interval; /* interval for interrupt endpoints */9898- int8_t in_ep; 9999- uint8_t in_interval;100100- uint16_t out_cables; /* bitmask */101101- uint16_t in_cables; /* bitmask */102102-};103103-104104-/* for QUIRK_MIDI_YAMAHA, data is NULL */105105-106106-/* for QUIRK_MIDI_MIDIMAN, data points to a snd_usb_midi_endpoint_info107107- * structure (out_cables and in_cables only) */108108-109109-/* for QUIRK_COMPOSITE, data points to an array of snd_usb_audio_quirk110110- * structures, terminated with .ifnum = -1 */111111-112112-/* for QUIRK_AUDIO_FIXED_ENDPOINT, data points to an audioformat structure */113113-114114-/* for QUIRK_AUDIO/MIDI_STANDARD_INTERFACE, data is NULL */115115-116116-/* for QUIRK_AUDIO_EDIROL_UAXX, data is NULL */117117-118118-/* for QUIRK_IGNORE_INTERFACE, data is NULL */119119-120120-/* for QUIRK_MIDI_NOVATION and _RAW, data is NULL */121121-122122-/* for QUIRK_MIDI_EMAGIC, data points to a snd_usb_midi_endpoint_info123123- * structure (out_cables and in_cables only) */124124-125125-/* for QUIRK_MIDI_CME, data is NULL */126126-127127-/*128128- */129129-130130-/*E-mu USB samplerate control quirk*/131131-enum {132132- EMU_QUIRK_SR_44100HZ = 0,133133- EMU_QUIRK_SR_48000HZ,134134- EMU_QUIRK_SR_88200HZ,135135- EMU_QUIRK_SR_96000HZ,136136- EMU_QUIRK_SR_176400HZ,137137- EMU_QUIRK_SR_192000HZ138138-};139139-14092#define combine_word(s) ((*(s)) | ((unsigned int)(s)[1] << 8))14193#define combine_triple(s) (combine_word(s) | ((unsigned int)(s)[2] << 16))14294#define combine_quad(s) (combine_triple(s) | ((unsigned int)(s)[3] << 24))143143-144144-unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size);145145-146146-void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype);147147-void *snd_usb_find_csint_desc(void *descstart, int desclen, void *after, u8 dsubtype);148148-149149-int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe,150150- __u8 request, __u8 requesttype, __u16 value, __u16 index,151151- void *data, __u16 size, int timeout);152152-153153-int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,154154- int ignore_error);155155-void snd_usb_mixer_disconnect(struct list_head *p);156156-157157-int snd_usbmidi_create(struct snd_card *card,158158- struct usb_interface *iface,159159- struct list_head *midi_list,160160- const struct snd_usb_audio_quirk *quirk);161161-void snd_usbmidi_input_stop(struct list_head* p);162162-void snd_usbmidi_input_start(struct list_head* p);163163-void snd_usbmidi_disconnect(struct list_head *p);164164-165165-void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,166166- unsigned char samplerate_id);167167-168168-/*169169- * retrieve usb_interface descriptor from the host interface170170- * (conditional for compatibility with the older API)171171- */172172-#ifndef get_iface_desc173173-#define get_iface_desc(iface) (&(iface)->desc)174174-#define get_endpoint(alt,ep) (&(alt)->endpoint[ep].desc)175175-#define get_ep_desc(ep) (&(ep)->desc)176176-#define get_cfg_desc(cfg) (&(cfg)->desc)177177-#endif178178-179179-#ifndef snd_usb_get_speed180180-#define snd_usb_get_speed(dev) ((dev)->speed)181181-#endif1829518396#endif /* __USBAUDIO_H */
+20-7
sound/usb/usbmidi.c
sound/usb/midi.c
···5353#include <sound/rawmidi.h>5454#include <sound/asequencer.h>5555#include "usbaudio.h"5656-5656+#include "midi.h"5757+#include "helper.h"57585859/*5960 * define this to log all USB packets···987986 DEFINE_WAIT(wait);988987 long timeout = msecs_to_jiffies(50);989988989989+ if (ep->umidi->disconnected)990990+ return;990991 /*991992 * The substream buffer is empty, but some data might still be in the992993 * currently active URBs, so we have to wait for those to complete.···11261123 * Frees an output endpoint.11271124 * May be called when ep hasn't been initialized completely.11281125 */11291129-static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)11261126+static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)11301127{11311128 unsigned int i;1132112911331130 for (i = 0; i < OUTPUT_URBS; ++i)11341134- if (ep->urbs[i].urb)11311131+ if (ep->urbs[i].urb) {11351132 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,11361133 ep->max_transfer);11341134+ ep->urbs[i].urb = NULL;11351135+ }11361136+}11371137+11381138+static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)11391139+{11401140+ snd_usbmidi_out_endpoint_clear(ep);11371141 kfree(ep);11381142}11391143···12721262 usb_kill_urb(ep->out->urbs[j].urb);12731263 if (umidi->usb_protocol_ops->finish_out_endpoint)12741264 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);12651265+ ep->out->active_urbs = 0;12661266+ if (ep->out->drain_urbs) {12671267+ ep->out->drain_urbs = 0;12681268+ wake_up(&ep->out->drain_wait);12691269+ }12751270 }12761271 if (ep->in)12771272 for (j = 0; j < INPUT_URBS; ++j)12781273 usb_kill_urb(ep->in->urbs[j]);12791274 /* free endpoints here; later call can result in Oops */12801280- if (ep->out) {12811281- snd_usbmidi_out_endpoint_delete(ep->out);12821282- ep->out = NULL;12831283- }12751275+ if (ep->out)12761276+ snd_usbmidi_out_endpoint_clear(ep->out);12841277 if (ep->in) {12851278 snd_usbmidi_in_endpoint_delete(ep->in);12861279 ep->in = NULL;
+300-547
sound/usb/usbmixer.c
sound/usb/mixer.c
···3333#include <linux/string.h>3434#include <linux/usb.h>3535#include <linux/usb/audio.h>3636+#include <linux/usb/audio-v2.h>36373738#include <sound/core.h>3839#include <sound/control.h>···4241#include <sound/tlv.h>43424443#include "usbaudio.h"4545-4646-/*4747- */4848-4949-/* ignore error from controls - for debugging */5050-/* #define IGNORE_CTL_ERROR */5151-5252-/*5353- * Sound Blaster remote control configuration5454- *5555- * format of remote control data:5656- * Extigy: xx 005757- * Audigy 2 NX: 06 80 xx 00 00 005858- * Live! 24-bit: 06 80 xx yy 22 835959- */6060-static const struct rc_config {6161- u32 usb_id;6262- u8 offset;6363- u8 length;6464- u8 packet_length;6565- u8 min_packet_length; /* minimum accepted length of the URB result */6666- u8 mute_mixer_id;6767- u32 mute_code;6868-} rc_configs[] = {6969- { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */7070- { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */7171- { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */7272- { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */7373-};4444+#include "mixer.h"4545+#include "helper.h"4646+#include "mixer_quirks.h"74477548#define MAX_ID_ELEMS 2567676-7777-struct usb_mixer_interface {7878- struct snd_usb_audio *chip;7979- unsigned int ctrlif;8080- struct list_head list;8181- unsigned int ignore_ctl_error;8282- struct urb *urb;8383- /* array[MAX_ID_ELEMS], indexed by unit id */8484- struct usb_mixer_elem_info **id_elems;8585-8686- /* Sound Blaster remote control stuff */8787- const struct rc_config *rc_cfg;8888- u32 rc_code;8989- wait_queue_head_t rc_waitq;9090- struct urb *rc_urb;9191- struct usb_ctrlrequest *rc_setup_packet;9292- u8 rc_buffer[6];9393-9494- u8 audigy2nx_leds[3];9595- u8 xonar_u1_status;9696-};9797-98499950struct usb_audio_term {10051 int id;···67114 struct usb_audio_term oterm;68115 const struct usbmix_name_map *map;69116 const struct usbmix_selector_map *selector_map;7070-};7171-7272-#define MAX_CHANNELS 10 /* max logical channels */7373-7474-struct usb_mixer_elem_info {7575- struct usb_mixer_interface *mixer;7676- struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */7777- struct snd_ctl_elem_id *elem_id;7878- unsigned int id;7979- unsigned int control; /* CS or ICN (high byte) */8080- unsigned int cmask; /* channel mask bitmap: 0 = master */8181- int channels;8282- int val_type;8383- int min, max, res;8484- int dBmin, dBmax;8585- int cached;8686- int cache_val[MAX_CHANNELS];8787- u8 initialized;8888-};8989-9090-9191-enum {9292- USB_FEATURE_NONE = 0,9393- USB_FEATURE_MUTE = 1,9494- USB_FEATURE_VOLUME,9595- USB_FEATURE_BASS,9696- USB_FEATURE_MID,9797- USB_FEATURE_TREBLE,9898- USB_FEATURE_GEQ,9999- USB_FEATURE_AGC,100100- USB_FEATURE_DELAY,101101- USB_FEATURE_BASSBOOST,102102- USB_FEATURE_LOUDNESS103117};104118105119enum {···133213 * if the mixer topology is too complicated and the parsed names are134214 * ambiguous, add the entries in usbmixer_maps.c.135215 */136136-#include "usbmixer_maps.c"216216+#include "mixer_maps.c"137217138218static const struct usbmix_name_map *139219find_map(struct mixer_build *state, int unitid, int control)···198278199279/*200280 * find an audio control unit with the given unit id281281+ * this doesn't return any clock related units, so they need to be handled elsewhere201282 */202283static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)203284{···207286 p = NULL;208287 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,209288 USB_DT_CS_INTERFACE)) != NULL) {210210- if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC_EXTENSION_UNIT_V1 && p[3] == unit)289289+ if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC2_EXTENSION_UNIT_V2 && p[3] == unit)211290 return p;212291 }213292 return NULL;···304383 * retrieve a mixer value305384 */306385307307-static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)386386+static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)308387{309388 unsigned char buf[2];310389 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;···324403 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",325404 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);326405 return -EINVAL;406406+}407407+408408+static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)409409+{410410+ unsigned char buf[14]; /* enough space for one range of 4 bytes */411411+ unsigned char *val;412412+ int ret;413413+ __u8 bRequest;414414+415415+ bRequest = (request == UAC_GET_CUR) ?416416+ UAC2_CS_CUR : UAC2_CS_RANGE;417417+418418+ ret = snd_usb_ctl_msg(cval->mixer->chip->dev,419419+ usb_rcvctrlpipe(cval->mixer->chip->dev, 0),420420+ bRequest,421421+ USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,422422+ validx, cval->mixer->ctrlif | (cval->id << 8),423423+ buf, sizeof(buf), 1000);424424+425425+ if (ret < 0) {426426+ snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",427427+ request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);428428+ return ret;429429+ }430430+431431+ switch (request) {432432+ case UAC_GET_CUR:433433+ val = buf;434434+ break;435435+ case UAC_GET_MIN:436436+ val = buf + sizeof(__u16);437437+ break;438438+ case UAC_GET_MAX:439439+ val = buf + sizeof(__u16) * 2;440440+ break;441441+ case UAC_GET_RES:442442+ val = buf + sizeof(__u16) * 3;443443+ break;444444+ default:445445+ return -EINVAL;446446+ }447447+448448+ *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16)));449449+450450+ return 0;451451+}452452+453453+static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)454454+{455455+ return (cval->mixer->protocol == UAC_VERSION_1) ?456456+ get_ctl_value_v1(cval, request, validx, value_ret) :457457+ get_ctl_value_v2(cval, request, validx, value_ret);327458}328459329460static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)···402429 err = get_cur_mix_raw(cval, channel, value);403430 if (err < 0) {404431 if (!cval->mixer->ignore_ctl_error)405405- snd_printd(KERN_ERR "cannot get current value for "406406- "control %d ch %d: err = %d\n",432432+ snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n",407433 cval->control, channel, err);408434 return err;409435 }···416444 * set a mixer value417445 */418446419419-static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)447447+int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,448448+ int request, int validx, int value_set)420449{421450 unsigned char buf[2];422422- int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;423423- int timeout = 10;451451+ int val_len, timeout = 10;452452+453453+ if (cval->mixer->protocol == UAC_VERSION_1) {454454+ val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;455455+ } else { /* UAC_VERSION_2 */456456+ /* audio class v2 controls are always 2 bytes in size */457457+ val_len = sizeof(__u16);458458+459459+ /* FIXME */460460+ if (request != UAC_SET_CUR) {461461+ snd_printdd(KERN_WARNING "RANGE setting not yet supported\n");462462+ return -EINVAL;463463+ }464464+465465+ request = UAC2_CS_CUR;466466+ }424467425468 value_set = convert_bytes_value(cval, value_set);426469 buf[0] = value_set & 0xff;···455468456469static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)457470{458458- return set_ctl_value(cval, UAC_SET_CUR, validx, value);471471+ return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);459472}460473461474static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,462475 int index, int value)463476{464477 int err;465465- err = set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,478478+ err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,466479 value);467480 if (err < 0)468481 return err;···631644 */632645static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)633646{634634- unsigned char *p1;647647+ void *p1;635648636649 memset(term, 0, sizeof(*term));637650 while ((p1 = find_audio_control_unit(state, id)) != NULL) {651651+ unsigned char *hdr = p1;638652 term->id = id;639639- switch (p1[2]) {653653+ switch (hdr[2]) {640654 case UAC_INPUT_TERMINAL:641641- term->type = combine_word(p1 + 4);642642- term->channels = p1[7];643643- term->chconfig = combine_word(p1 + 8);644644- term->name = p1[11];655655+ if (state->mixer->protocol == UAC_VERSION_1) {656656+ struct uac_input_terminal_descriptor *d = p1;657657+ term->type = le16_to_cpu(d->wTerminalType);658658+ term->channels = d->bNrChannels;659659+ term->chconfig = le16_to_cpu(d->wChannelConfig);660660+ term->name = d->iTerminal;661661+ } else { /* UAC_VERSION_2 */662662+ struct uac2_input_terminal_descriptor *d = p1;663663+ term->type = le16_to_cpu(d->wTerminalType);664664+ term->channels = d->bNrChannels;665665+ term->chconfig = le32_to_cpu(d->bmChannelConfig);666666+ term->name = d->iTerminal;667667+ }645668 return 0;646646- case UAC_FEATURE_UNIT:647647- id = p1[4];669669+ case UAC_FEATURE_UNIT: {670670+ /* the header is the same for v1 and v2 */671671+ struct uac_feature_unit_descriptor *d = p1;672672+ id = d->bUnitID;648673 break; /* continue to parse */649649- case UAC_MIXER_UNIT:650650- term->type = p1[2] << 16; /* virtual type */651651- term->channels = p1[5 + p1[4]];652652- term->chconfig = combine_word(p1 + 6 + p1[4]);653653- term->name = p1[p1[0] - 1];674674+ }675675+ case UAC_MIXER_UNIT: {676676+ struct uac_mixer_unit_descriptor *d = p1;677677+ term->type = d->bDescriptorSubtype << 16; /* virtual type */678678+ term->channels = uac_mixer_unit_bNrChannels(d);679679+ term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);680680+ term->name = uac_mixer_unit_iMixer(d);654681 return 0;655655- case UAC_SELECTOR_UNIT:682682+ }683683+ case UAC_SELECTOR_UNIT: {684684+ struct uac_selector_unit_descriptor *d = p1;656685 /* call recursively to retrieve the channel info */657657- if (check_input_term(state, p1[5], term) < 0)686686+ if (check_input_term(state, d->baSourceID[0], term) < 0)658687 return -ENODEV;659659- term->type = p1[2] << 16; /* virtual type */688688+ term->type = d->bDescriptorSubtype << 16; /* virtual type */660689 term->id = id;661661- term->name = p1[9 + p1[0] - 1];690690+ term->name = uac_selector_unit_iSelector(d);662691 return 0;692692+ }663693 case UAC_PROCESSING_UNIT_V1:664664- case UAC_EXTENSION_UNIT_V1:665665- if (p1[6] == 1) {666666- id = p1[7];694694+ case UAC_EXTENSION_UNIT_V1: {695695+ struct uac_processing_unit_descriptor *d = p1;696696+ if (d->bNrInPins) {697697+ id = d->baSourceID[0];667698 break; /* continue to parse */668699 }669669- term->type = p1[2] << 16; /* virtual type */670670- term->channels = p1[7 + p1[6]];671671- term->chconfig = combine_word(p1 + 8 + p1[6]);672672- term->name = p1[12 + p1[6] + p1[11 + p1[6]]];700700+ term->type = d->bDescriptorSubtype << 16; /* virtual type */701701+ term->channels = uac_processing_unit_bNrChannels(d);702702+ term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);703703+ term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);673704 return 0;705705+ }674706 default:675707 return -ENODEV;676708 }···770764 int last_valid_res = cval->res;771765772766 while (cval->res > 1) {773773- if (set_ctl_value(cval, UAC_SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)767767+ if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,768768+ (cval->control << 8) | minchn, cval->res / 2) < 0)774769 break;775770 cval->res /= 2;776771 }···936929 .put = mixer_ctl_feature_put,937930};938931932932+/* the read-only variant */933933+static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {934934+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,935935+ .name = "", /* will be filled later manually */936936+ .info = mixer_ctl_feature_info,937937+ .get = mixer_ctl_feature_get,938938+ .put = NULL,939939+};940940+939941940942/*941943 * build a feature control···955939 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));956940}957941958958-static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,942942+static void build_feature_ctl(struct mixer_build *state, void *raw_desc,959943 unsigned int ctl_mask, int control,960960- struct usb_audio_term *iterm, int unitid)944944+ struct usb_audio_term *iterm, int unitid,945945+ int read_only)961946{947947+ struct uac_feature_unit_descriptor *desc = raw_desc;962948 unsigned int len = 0;963949 int mapped_name = 0;964964- int nameid = desc[desc[0] - 1];950950+ int nameid = uac_feature_unit_iFeature(desc);965951 struct snd_kcontrol *kctl;966952 struct usb_mixer_elem_info *cval;967953 const struct usbmix_name_map *map;968954969955 control++; /* change from zero-based to 1-based value */970956971971- if (control == USB_FEATURE_GEQ) {957957+ if (control == UAC_GRAPHIC_EQUALIZER_CONTROL) {972958 /* FIXME: not supported yet */973959 return;974960 }···1002984 /* get min/max values */1003985 get_min_max(cval, 0);100498610051005- kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);987987+ if (read_only)988988+ kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);989989+ else990990+ kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);991991+1006992 if (! kctl) {1007993 snd_printk(KERN_ERR "cannot malloc kcontrol\n");1008994 kfree(cval);···1021999 kctl->id.name, sizeof(kctl->id.name));1022100010231001 switch (control) {10241024- case USB_FEATURE_MUTE:10251025- case USB_FEATURE_VOLUME:10021002+ case UAC_MUTE_CONTROL:10031003+ case UAC_VOLUME_CONTROL:10261004 /* determine the control name. the rule is:10271005 * - if a name id is given in descriptor, use it.10281006 * - if the connected input can be determined, then use the name···10491027 len = append_ctl_name(kctl, " Playback");10501028 }10511029 }10521052- append_ctl_name(kctl, control == USB_FEATURE_MUTE ?10301030+ append_ctl_name(kctl, control == UAC_MUTE_CONTROL ?10531031 " Switch" : " Volume");10541054- if (control == USB_FEATURE_VOLUME) {10321032+ if (control == UAC_VOLUME_CONTROL) {10551033 kctl->tlv.c = mixer_vol_tlv;10561034 kctl->vd[0].access |= 10571035 SNDRV_CTL_ELEM_ACCESS_TLV_READ |···11161094 struct usb_audio_term iterm;11171095 unsigned int master_bits, first_ch_bits;11181096 int err, csize;11191119- struct uac_feature_unit_descriptor *ftr = _ftr;10971097+ struct uac_feature_unit_descriptor *hdr = _ftr;10981098+ __u8 *bmaControls;1120109911211121- if (ftr->bLength < 7 || ! (csize = ftr->bControlSize) || ftr->bLength < 7 + csize) {11001100+ if (state->mixer->protocol == UAC_VERSION_1) {11011101+ csize = hdr->bControlSize;11021102+ channels = (hdr->bLength - 7) / csize - 1;11031103+ bmaControls = hdr->bmaControls;11041104+ } else {11051105+ struct uac2_feature_unit_descriptor *ftr = _ftr;11061106+ csize = 4;11071107+ channels = (hdr->bLength - 6) / 4;11081108+ bmaControls = ftr->bmaControls;11091109+ }11101110+11111111+ if (hdr->bLength < 7 || !csize || hdr->bLength < 7 + csize) {11221112 snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);11231113 return -EINVAL;11241114 }1125111511261116 /* parse the source unit */11271127- if ((err = parse_audio_unit(state, ftr->bSourceID)) < 0)11171117+ if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)11281118 return err;1129111911301120 /* determine the input source type and name */11311131- if (check_input_term(state, ftr->bSourceID, &iterm) < 0)11211121+ if (check_input_term(state, hdr->bSourceID, &iterm) < 0)11321122 return -EINVAL;1133112311341134- channels = (ftr->bLength - 7) / csize - 1;11351135-11361136- master_bits = snd_usb_combine_bytes(ftr->controls, csize);11241124+ master_bits = snd_usb_combine_bytes(bmaControls, csize);11371125 /* master configuration quirks */11381126 switch (state->chip->usb_id) {11391127 case USB_ID(0x08bb, 0x2702):11401128 snd_printk(KERN_INFO11411129 "usbmixer: master volume quirk for PCM2702 chip\n");11421130 /* disable non-functional volume control */11431143- master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));11311131+ master_bits &= ~UAC_FU_VOLUME;11441132 break;11451133 }11461134 if (channels > 0)11471147- first_ch_bits = snd_usb_combine_bytes(ftr->controls + csize, csize);11351135+ first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);11481136 else11491137 first_ch_bits = 0;11501150- /* check all control types */11511151- for (i = 0; i < 10; i++) {11521152- unsigned int ch_bits = 0;11531153- for (j = 0; j < channels; j++) {11541154- unsigned int mask = snd_usb_combine_bytes(ftr->controls + csize * (j+1), csize);11551155- if (mask & (1 << i))11561156- ch_bits |= (1 << j);11381138+11391139+ if (state->mixer->protocol == UAC_VERSION_1) {11401140+ /* check all control types */11411141+ for (i = 0; i < 10; i++) {11421142+ unsigned int ch_bits = 0;11431143+ for (j = 0; j < channels; j++) {11441144+ unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);11451145+ if (mask & (1 << i))11461146+ ch_bits |= (1 << j);11471147+ }11481148+ /* audio class v1 controls are never read-only */11491149+ if (ch_bits & 1) /* the first channel must be set (for ease of programming) */11501150+ build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0);11511151+ if (master_bits & (1 << i))11521152+ build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0);11571153 }11581158- if (ch_bits & 1) /* the first channel must be set (for ease of programming) */11591159- build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid);11601160- if (master_bits & (1 << i))11611161- build_feature_ctl(state, _ftr, 0, i, &iterm, unitid);11541154+ } else { /* UAC_VERSION_2 */11551155+ for (i = 0; i < 30/2; i++) {11561156+ /* From the USB Audio spec v2.0:11571157+ bmaControls() is a (ch+1)-element array of 4-byte bitmaps,11581158+ each containing a set of bit pairs. If a Control is present,11591159+ it must be Host readable. If a certain Control is not11601160+ present then the bit pair must be set to 0b00.11611161+ If a Control is present but read-only, the bit pair must be11621162+ set to 0b01. If a Control is also Host programmable, the bit11631163+ pair must be set to 0b11. The value 0b10 is not allowed. */11641164+ unsigned int ch_bits = 0;11651165+ unsigned int ch_read_only = 0;11661166+11671167+ for (j = 0; j < channels; j++) {11681168+ unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);11691169+ if (mask & (1 << (i * 2))) {11701170+ ch_bits |= (1 << j);11711171+ if (~mask & (1 << ((i * 2) + 1)))11721172+ ch_read_only |= (1 << j);11731173+ }11741174+ }11751175+11761176+ /* FIXME: the whole unit is read-only if any of the channels is marked read-only */11771177+ if (ch_bits & 1) /* the first channel must be set (for ease of programming) */11781178+ build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, !!ch_read_only);11791179+ if (master_bits & (1 << i * 2))11801180+ build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,11811181+ ~master_bits & (1 << ((i * 2) + 1)));11821182+ }11621183 }1163118411641185 return 0;···12191154 * input channel number (zero based) is given in control field instead.12201155 */1221115612221222-static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,11571157+static void build_mixer_unit_ctl(struct mixer_build *state,11581158+ struct uac_mixer_unit_descriptor *desc,12231159 int in_pin, int in_ch, int unitid,12241160 struct usb_audio_term *iterm)12251161{12261162 struct usb_mixer_elem_info *cval;12271227- unsigned int input_pins = desc[4];12281228- unsigned int num_outs = desc[5 + input_pins];11631163+ unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);12291164 unsigned int i, len;12301165 struct snd_kcontrol *kctl;12311166 const struct usbmix_name_map *map;···12431178 cval->control = in_ch + 1; /* based on 1 */12441179 cval->val_type = USB_MIXER_S16;12451180 for (i = 0; i < num_outs; i++) {12461246- if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {11811181+ if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) {12471182 cval->cmask |= (1 << i);12481183 cval->channels++;12491184 }···12761211/*12771212 * parse a mixer unit12781213 */12791279-static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)12141214+static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc)12801215{12161216+ struct uac_mixer_unit_descriptor *desc = raw_desc;12811217 struct usb_audio_term iterm;12821218 int input_pins, num_ins, num_outs;12831219 int pin, ich, err;1284122012851285- if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {12211221+ if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) {12861222 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);12871223 return -EINVAL;12881224 }12891225 /* no bmControls field (e.g. Maya44) -> ignore */12901290- if (desc[0] <= 10 + input_pins) {12261226+ if (desc->bLength <= 10 + input_pins) {12911227 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);12921228 return 0;12931229 }···12961230 num_ins = 0;12971231 ich = 0;12981232 for (pin = 0; pin < input_pins; pin++) {12991299- err = parse_audio_unit(state, desc[5 + pin]);12331233+ err = parse_audio_unit(state, desc->baSourceID[pin]);13001234 if (err < 0)13011235 return err;13021302- err = check_input_term(state, desc[5 + pin], &iterm);12361236+ err = check_input_term(state, desc->baSourceID[pin], &iterm);13031237 if (err < 0)13041238 return err;13051239 num_ins += iterm.channels;···13071241 int och, ich_has_controls = 0;1308124213091243 for (och = 0; och < num_outs; ++och) {13101310- if (check_matrix_bitmap(desc + 9 + input_pins,12441244+ if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol),13111245 ich, och, num_outs)) {13121246 ich_has_controls = 1;13131247 break;···14681402/*14691403 * build a processing/extension unit14701404 */14711471-static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)14051405+static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name)14721406{14731473- int num_ins = dsc[6];14071407+ struct uac_processing_unit_descriptor *desc = raw_desc;14081408+ int num_ins = desc->bNrInPins;14741409 struct usb_mixer_elem_info *cval;14751410 struct snd_kcontrol *kctl;14761411 int i, err, nameid, type, len;···14861419 0, NULL, default_value_info14871420 };1488142114891489- if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {14221422+ if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||14231423+ desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {14901424 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);14911425 return -EINVAL;14921426 }1493142714941428 for (i = 0; i < num_ins; i++) {14951495- if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)14291429+ if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)14961430 return err;14971431 }1498143214991499- type = combine_word(&dsc[4]);14331433+ type = le16_to_cpu(desc->wProcessType);15001434 for (info = list; info && info->type; info++)15011435 if (info->type == type)15021436 break;···15051437 info = &default_info;1506143815071439 for (valinfo = info->values; valinfo->control; valinfo++) {15081508- /* FIXME: bitmap might be longer than 8bit */15091509- if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))14401440+ __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);14411441+14421442+ if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))15101443 continue;15111444 map = find_map(state, unitid, valinfo->control);15121445 if (check_ignored_ctl(map))···1525145615261457 /* get min/max values */15271458 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {14591459+ __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);15281460 /* FIXME: hard-coded */15291461 cval->min = 1;15301530- cval->max = dsc[15];14621462+ cval->max = control_spec[0];15311463 cval->res = 1;15321464 cval->initialized = 1;15331465 } else {···15581488 else if (info->name)15591489 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));15601490 else {15611561- nameid = dsc[12 + num_ins + dsc[11 + num_ins]];14911491+ nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);15621492 len = 0;15631493 if (nameid)15641494 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));···15771507}157815081579150915801580-static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)15101510+static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc)15811511{15821582- return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");15121512+ return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit");15831513}1584151415851585-static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)15151515+static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc)15861516{15871587- return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");15171517+ /* Note that we parse extension units with processing unit descriptors.15181518+ * That's ok as the layout is the same */15191519+ return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit");15881520}1589152115901522···16881616/*16891617 * parse a selector unit16901618 */16911691-static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)16191619+static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc)16921620{16931693- unsigned int num_ins = desc[4];16211621+ struct uac_selector_unit_descriptor *desc = raw_desc;16941622 unsigned int i, nameid, len;16951623 int err;16961624 struct usb_mixer_elem_info *cval;···16981626 const struct usbmix_name_map *map;16991627 char **namelist;1700162817011701- if (! num_ins || desc[0] < 5 + num_ins) {16291629+ if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {17021630 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);17031631 return -EINVAL;17041632 }1705163317061706- for (i = 0; i < num_ins; i++) {17071707- if ((err = parse_audio_unit(state, desc[5 + i])) < 0)16341634+ for (i = 0; i < desc->bNrInPins; i++) {16351635+ if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)17081636 return err;17091637 }1710163817111711- if (num_ins == 1) /* only one ? nonsense! */16391639+ if (desc->bNrInPins == 1) /* only one ? nonsense! */17121640 return 0;1713164117141642 map = find_map(state, unitid, 0);···17251653 cval->val_type = USB_MIXER_U8;17261654 cval->channels = 1;17271655 cval->min = 1;17281728- cval->max = num_ins;16561656+ cval->max = desc->bNrInPins;17291657 cval->res = 1;17301658 cval->initialized = 1;1731165917321732- namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);16601660+ namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);17331661 if (! namelist) {17341662 snd_printk(KERN_ERR "cannot malloc\n");17351663 kfree(cval);17361664 return -ENOMEM;17371665 }17381666#define MAX_ITEM_NAME_LEN 6417391739- for (i = 0; i < num_ins; i++) {16671667+ for (i = 0; i < desc->bNrInPins; i++) {17401668 struct usb_audio_term iterm;17411669 len = 0;17421670 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);···17501678 }17511679 len = check_mapped_selector_name(state, unitid, i, namelist[i],17521680 MAX_ITEM_NAME_LEN);17531753- if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)16811681+ if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)17541682 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);17551683 if (! len)17561684 sprintf(namelist[i], "Input %d", i);···17661694 kctl->private_value = (unsigned long)namelist;17671695 kctl->private_free = usb_mixer_selector_elem_free;1768169617691769- nameid = desc[desc[0] - 1];16971697+ nameid = uac_selector_unit_iSelector(desc);17701698 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));17711699 if (len)17721700 ;···17851713 }1786171417871715 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",17881788- cval->id, kctl->id.name, num_ins);17161716+ cval->id, kctl->id.name, desc->bNrInPins);17891717 if ((err = add_control_to_empty(state, kctl)) < 0)17901718 return err;17911719···18201748 case UAC_FEATURE_UNIT:18211749 return parse_audio_feature_unit(state, unitid, p1);18221750 case UAC_PROCESSING_UNIT_V1:18231823- return parse_audio_processing_unit(state, unitid, p1);17511751+ /* UAC2_EFFECT_UNIT has the same value */17521752+ if (state->mixer->protocol == UAC_VERSION_1)17531753+ return parse_audio_processing_unit(state, unitid, p1);17541754+ else17551755+ return 0; /* FIXME - effect units not implemented yet */18241756 case UAC_EXTENSION_UNIT_V1:18251825- return parse_audio_extension_unit(state, unitid, p1);17571757+ /* UAC2_PROCESSING_UNIT_V2 has the same value */17581758+ if (state->mixer->protocol == UAC_VERSION_1)17591759+ return parse_audio_extension_unit(state, unitid, p1);17601760+ else /* UAC_VERSION_2 */17611761+ return parse_audio_processing_unit(state, unitid, p1);18261762 default:18271763 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);18281764 return -EINVAL;···18631783 */18641784static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)18651785{18661866- struct uac_output_terminal_descriptor_v1 *desc;18671786 struct mixer_build state;18681787 int err;18691788 const struct usbmix_ctl_map *map;18701789 struct usb_host_interface *hostif;17901790+ void *p;1871179118721792 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];18731793 memset(&state, 0, sizeof(state));···18861806 }18871807 }1888180818891889- desc = NULL;18901890- while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, UAC_OUTPUT_TERMINAL)) != NULL) {18911891- if (desc->bLength < 9)18921892- continue; /* invalid descriptor? */18931893- set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */18941894- state.oterm.id = desc->bTerminalID;18951895- state.oterm.type = le16_to_cpu(desc->wTerminalType);18961896- state.oterm.name = desc->iTerminal;18971897- err = parse_audio_unit(&state, desc->bSourceID);18981898- if (err < 0)18991899- return err;18091809+ p = NULL;18101810+ while ((p = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, p, UAC_OUTPUT_TERMINAL)) != NULL) {18111811+ if (mixer->protocol == UAC_VERSION_1) {18121812+ struct uac_output_terminal_descriptor_v1 *desc = p;18131813+18141814+ if (desc->bLength < sizeof(*desc))18151815+ continue; /* invalid descriptor? */18161816+ set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */18171817+ state.oterm.id = desc->bTerminalID;18181818+ state.oterm.type = le16_to_cpu(desc->wTerminalType);18191819+ state.oterm.name = desc->iTerminal;18201820+ err = parse_audio_unit(&state, desc->bSourceID);18211821+ if (err < 0)18221822+ return err;18231823+ } else { /* UAC_VERSION_2 */18241824+ struct uac2_output_terminal_descriptor *desc = p;18251825+18261826+ if (desc->bLength < sizeof(*desc))18271827+ continue; /* invalid descriptor? */18281828+ set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */18291829+ state.oterm.id = desc->bTerminalID;18301830+ state.oterm.type = le16_to_cpu(desc->wTerminalType);18311831+ state.oterm.name = desc->iTerminal;18321832+ err = parse_audio_unit(&state, desc->bSourceID);18331833+ if (err < 0)18341834+ return err;18351835+ }19001836 }18371837+19011838 return 0;19021839}1903184019041904-static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,19051905- int unitid)18411841+void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)19061842{19071843 struct usb_mixer_elem_info *info;19081844···19671871 }19681872}1969187319701970-static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,19711971- int unitid)19721972-{19731973- if (!mixer->rc_cfg)19741974- return;19751975- /* unit ids specific to Extigy/Audigy 2 NX: */19761976- switch (unitid) {19771977- case 0: /* remote control */19781978- mixer->rc_urb->dev = mixer->chip->dev;19791979- usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);19801980- break;19811981- case 4: /* digital in jack */19821982- case 7: /* line in jacks */19831983- case 19: /* speaker out jacks */19841984- case 20: /* headphones out jack */19851985- break;19861986- /* live24ext: 4 = line-in jack */19871987- case 3: /* hp-out jack (may actuate Mute) */19881988- if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||19891989- mixer->chip->usb_id == USB_ID(0x041e, 0x3048))19901990- snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);19911991- break;19921992- default:19931993- snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);19941994- break;19951995- }19961996-}19971997-19981874static void snd_usb_mixer_status_complete(struct urb *urb)19991875{20001876 struct usb_mixer_interface *mixer = urb->context;···19841916 if (!(buf[0] & 0x40))19851917 snd_usb_mixer_notify_id(mixer, buf[1]);19861918 else19871987- snd_usb_mixer_memory_change(mixer, buf[1]);19191919+ snd_usb_mixer_rc_memory_change(mixer, buf[1]);19881920 }19891921 }19901922 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {···20281960 return 0;20291961}2030196220312031-static void snd_usb_soundblaster_remote_complete(struct urb *urb)20322032-{20332033- struct usb_mixer_interface *mixer = urb->context;20342034- const struct rc_config *rc = mixer->rc_cfg;20352035- u32 code;20362036-20372037- if (urb->status < 0 || urb->actual_length < rc->min_packet_length)20382038- return;20392039-20402040- code = mixer->rc_buffer[rc->offset];20412041- if (rc->length == 2)20422042- code |= mixer->rc_buffer[rc->offset + 1] << 8;20432043-20442044- /* the Mute button actually changes the mixer control */20452045- if (code == rc->mute_code)20462046- snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);20472047- mixer->rc_code = code;20482048- wmb();20492049- wake_up(&mixer->rc_waitq);20502050-}20512051-20522052-static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,20532053- long count, loff_t *offset)20542054-{20552055- struct usb_mixer_interface *mixer = hw->private_data;20562056- int err;20572057- u32 rc_code;20582058-20592059- if (count != 1 && count != 4)20602060- return -EINVAL;20612061- err = wait_event_interruptible(mixer->rc_waitq,20622062- (rc_code = xchg(&mixer->rc_code, 0)) != 0);20632063- if (err == 0) {20642064- if (count == 1)20652065- err = put_user(rc_code, buf);20662066- else20672067- err = put_user(rc_code, (u32 __user *)buf);20682068- }20692069- return err < 0 ? err : count;20702070-}20712071-20722072-static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,20732073- poll_table *wait)20742074-{20752075- struct usb_mixer_interface *mixer = hw->private_data;20762076-20772077- poll_wait(file, &mixer->rc_waitq, wait);20782078- return mixer->rc_code ? POLLIN | POLLRDNORM : 0;20792079-}20802080-20812081-static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)20822082-{20832083- struct snd_hwdep *hwdep;20842084- int err, len, i;20852085-20862086- for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)20872087- if (rc_configs[i].usb_id == mixer->chip->usb_id)20882088- break;20892089- if (i >= ARRAY_SIZE(rc_configs))20902090- return 0;20912091- mixer->rc_cfg = &rc_configs[i];20922092-20932093- len = mixer->rc_cfg->packet_length;20942094-20952095- init_waitqueue_head(&mixer->rc_waitq);20962096- err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);20972097- if (err < 0)20982098- return err;20992099- snprintf(hwdep->name, sizeof(hwdep->name),21002100- "%s remote control", mixer->chip->card->shortname);21012101- hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;21022102- hwdep->private_data = mixer;21032103- hwdep->ops.read = snd_usb_sbrc_hwdep_read;21042104- hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;21052105- hwdep->exclusive = 1;21062106-21072107- mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);21082108- if (!mixer->rc_urb)21092109- return -ENOMEM;21102110- mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);21112111- if (!mixer->rc_setup_packet) {21122112- usb_free_urb(mixer->rc_urb);21132113- mixer->rc_urb = NULL;21142114- return -ENOMEM;21152115- }21162116- mixer->rc_setup_packet->bRequestType =21172117- USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;21182118- mixer->rc_setup_packet->bRequest = UAC_GET_MEM;21192119- mixer->rc_setup_packet->wValue = cpu_to_le16(0);21202120- mixer->rc_setup_packet->wIndex = cpu_to_le16(0);21212121- mixer->rc_setup_packet->wLength = cpu_to_le16(len);21222122- usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,21232123- usb_rcvctrlpipe(mixer->chip->dev, 0),21242124- (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,21252125- snd_usb_soundblaster_remote_complete, mixer);21262126- return 0;21272127-}21282128-21292129-#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info21302130-21312131-static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)21322132-{21332133- struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);21342134- int index = kcontrol->private_value;21352135-21362136- ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];21372137- return 0;21382138-}21392139-21402140-static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)21412141-{21422142- struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);21432143- int index = kcontrol->private_value;21442144- int value = ucontrol->value.integer.value[0];21452145- int err, changed;21462146-21472147- if (value > 1)21482148- return -EINVAL;21492149- changed = value != mixer->audigy2nx_leds[index];21502150- err = snd_usb_ctl_msg(mixer->chip->dev,21512151- usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,21522152- USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,21532153- value, index + 2, NULL, 0, 100);21542154- if (err < 0)21552155- return err;21562156- mixer->audigy2nx_leds[index] = value;21572157- return changed;21582158-}21592159-21602160-static struct snd_kcontrol_new snd_audigy2nx_controls[] = {21612161- {21622162- .iface = SNDRV_CTL_ELEM_IFACE_MIXER,21632163- .name = "CMSS LED Switch",21642164- .info = snd_audigy2nx_led_info,21652165- .get = snd_audigy2nx_led_get,21662166- .put = snd_audigy2nx_led_put,21672167- .private_value = 0,21682168- },21692169- {21702170- .iface = SNDRV_CTL_ELEM_IFACE_MIXER,21712171- .name = "Power LED Switch",21722172- .info = snd_audigy2nx_led_info,21732173- .get = snd_audigy2nx_led_get,21742174- .put = snd_audigy2nx_led_put,21752175- .private_value = 1,21762176- },21772177- {21782178- .iface = SNDRV_CTL_ELEM_IFACE_MIXER,21792179- .name = "Dolby Digital LED Switch",21802180- .info = snd_audigy2nx_led_info,21812181- .get = snd_audigy2nx_led_get,21822182- .put = snd_audigy2nx_led_put,21832183- .private_value = 2,21842184- },21852185-};21862186-21872187-static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)21882188-{21892189- int i, err;21902190-21912191- for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {21922192- if (i > 1 && /* Live24ext has 2 LEDs only */21932193- (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||21942194- mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))21952195- break; 21962196- err = snd_ctl_add(mixer->chip->card,21972197- snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));21982198- if (err < 0)21992199- return err;22002200- }22012201- mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */22022202- return 0;22032203-}22042204-22052205-static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,22062206- struct snd_info_buffer *buffer)22072207-{22082208- static const struct sb_jack {22092209- int unitid;22102210- const char *name;22112211- } jacks_audigy2nx[] = {22122212- {4, "dig in "},22132213- {7, "line in"},22142214- {19, "spk out"},22152215- {20, "hph out"},22162216- {-1, NULL}22172217- }, jacks_live24ext[] = {22182218- {4, "line in"}, /* &1=Line, &2=Mic*/22192219- {3, "hph out"}, /* headphones */22202220- {0, "RC "}, /* last command, 6 bytes see rc_config above */22212221- {-1, NULL}22222222- };22232223- const struct sb_jack *jacks;22242224- struct usb_mixer_interface *mixer = entry->private_data;22252225- int i, err;22262226- u8 buf[3];22272227-22282228- snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);22292229- if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))22302230- jacks = jacks_audigy2nx;22312231- else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||22322232- mixer->chip->usb_id == USB_ID(0x041e, 0x3048))22332233- jacks = jacks_live24ext;22342234- else22352235- return;22362236-22372237- for (i = 0; jacks[i].name; ++i) {22382238- snd_iprintf(buffer, "%s: ", jacks[i].name);22392239- err = snd_usb_ctl_msg(mixer->chip->dev,22402240- usb_rcvctrlpipe(mixer->chip->dev, 0),22412241- UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |22422242- USB_RECIP_INTERFACE, 0,22432243- jacks[i].unitid << 8, buf, 3, 100);22442244- if (err == 3 && (buf[0] == 3 || buf[0] == 6))22452245- snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);22462246- else22472247- snd_iprintf(buffer, "?\n");22482248- }22492249-}22502250-22512251-static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,22522252- struct snd_ctl_elem_value *ucontrol)22532253-{22542254- struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);22552255-22562256- ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);22572257- return 0;22582258-}22592259-22602260-static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,22612261- struct snd_ctl_elem_value *ucontrol)22622262-{22632263- struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);22642264- u8 old_status, new_status;22652265- int err, changed;22662266-22672267- old_status = mixer->xonar_u1_status;22682268- if (ucontrol->value.integer.value[0])22692269- new_status = old_status | 0x02;22702270- else22712271- new_status = old_status & ~0x02;22722272- changed = new_status != old_status;22732273- err = snd_usb_ctl_msg(mixer->chip->dev,22742274- usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,22752275- USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,22762276- 50, 0, &new_status, 1, 100);22772277- if (err < 0)22782278- return err;22792279- mixer->xonar_u1_status = new_status;22802280- return changed;22812281-}22822282-22832283-static struct snd_kcontrol_new snd_xonar_u1_output_switch = {22842284- .iface = SNDRV_CTL_ELEM_IFACE_MIXER,22852285- .name = "Digital Playback Switch",22862286- .info = snd_ctl_boolean_mono_info,22872287- .get = snd_xonar_u1_switch_get,22882288- .put = snd_xonar_u1_switch_put,22892289-};22902290-22912291-static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)22922292-{22932293- int err;22942294-22952295- err = snd_ctl_add(mixer->chip->card,22962296- snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));22972297- if (err < 0)22982298- return err;22992299- mixer->xonar_u1_status = 0x05;23002300- return 0;23012301-}23022302-23032303-void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,23042304- unsigned char samplerate_id)23052305-{23062306- struct usb_mixer_interface *mixer;23072307- struct usb_mixer_elem_info *cval;23082308- int unitid = 12; /* SamleRate ExtensionUnit ID */23092309-23102310- list_for_each_entry(mixer, &chip->mixer_list, list) {23112311- cval = mixer->id_elems[unitid];23122312- if (cval) {23132313- set_cur_ctl_value(cval, cval->control << 8,23142314- samplerate_id);23152315- snd_usb_mixer_notify_id(mixer, unitid);23162316- }23172317- break;23182318- }23192319-}23202320-23211963int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,23221964 int ignore_error)23231965{···20372259 struct usb_mixer_interface *mixer;20382260 struct snd_info_entry *entry;20392261 struct usb_host_interface *host_iface;20402040- int err, protocol;22622262+ int err;2041226320422264 strcpy(chip->card->mixername, "USB Mixer");20432265···20552277 }2056227820572279 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];20582058- protocol = host_iface->desc.bInterfaceProtocol;20592059-20602060- /* FIXME! */20612061- if (protocol != UAC_VERSION_1) {20622062- snd_printk(KERN_WARNING "mixer interface protocol 0x%02x not yet supported\n",20632063- protocol);20642064- return 0;20652065- }22802280+ mixer->protocol = host_iface->desc.bInterfaceProtocol;2066228120672282 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||20682283 (err = snd_usb_mixer_status_create(mixer)) < 0)20692284 goto _error;2070228520712071- if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)20722072- goto _error;20732073-20742074- if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||20752075- mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||20762076- mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {20772077- if ((err = snd_audigy2nx_controls_create(mixer)) < 0)20782078- goto _error;20792079- if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))20802080- snd_info_set_text_ops(entry, mixer,20812081- snd_audigy2nx_proc_read);20822082- }20832083-20842084- if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||20852085- mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {20862086- err = snd_xonar_u1_controls_create(mixer);20872087- if (err < 0)20882088- goto _error;20892089- }22862286+ snd_usb_mixer_apply_create_quirk(mixer);2090228720912288 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);20922289 if (err < 0)···20822329void snd_usb_mixer_disconnect(struct list_head *p)20832330{20842331 struct usb_mixer_interface *mixer;20852085-23322332+20862333 mixer = list_entry(p, struct usb_mixer_interface, list);20872334 usb_kill_urb(mixer->urb);20882335 usb_kill_urb(mixer->rc_urb);
+2-2
sound/usb/usbmixer_maps.c
sound/usb/mixer_maps.c
···8585 /* 16: MU (w/o controls) */8686 { 17, NULL, 1 }, /* DISABLED: PU-switch (any effect?) */8787 { 17, "Channel Routing", 2 }, /* PU: mode select */8888- { 18, "Tone Control - Bass", USB_FEATURE_BASS }, /* FU */8989- { 18, "Tone Control - Treble", USB_FEATURE_TREBLE }, /* FU */8888+ { 18, "Tone Control - Bass", UAC_BASS_CONTROL }, /* FU */8989+ { 18, "Tone Control - Treble", UAC_TREBLE_CONTROL }, /* FU */9090 { 18, "Master Playback" }, /* FU; others */9191 /* 19: OT speaker */9292 /* 20: OT headphone */