···155155 * @jack: The jack to configure156156 * @parent: The device to set as parent for the jack.157157 *158158- * Set the parent for the jack input device in the device tree. This158158+ * Set the parent for the jack devices in the device tree. This159159 * function is only valid prior to registration of the jack. If no160160 * parent is configured then the parent device will be the sound card.161161 */···178178 * reporting of keys on accessories via the jack abstraction. If no179179 * mapping is provided but keys are enabled in the jack type then180180 * BTN_n numeric buttons will be reported.181181+ *182182+ * If jacks are not reporting via the input API this call will have no183183+ * effect.181184 *182185 * Note that this is intended to be use by simple devices with small183186 * numbers of keys that can be reported. It is also possible to
···3131#define INTERRUPT_INTERVAL 163232#define QUEUE_LENGTH 4833333434+static void pcm_period_tasklet(unsigned long data);3535+3436/**3537 * amdtp_out_stream_init - initialize an AMDTP output stream structure3638 * @s: the AMDTP output stream to initialize···4947 s->flags = flags;5048 s->context = ERR_PTR(-1);5149 mutex_init(&s->mutex);5050+ tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);5251 s->packet_index = 0;53525453 return 0;···166163 }167164}168165EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);166166+167167+/**168168+ * amdtp_out_stream_pcm_prepare - prepare PCM device for running169169+ * @s: the AMDTP output stream170170+ *171171+ * This function should be called from the PCM device's .prepare callback.172172+ */173173+void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)174174+{175175+ tasklet_kill(&s->period_tasklet);176176+ s->pcm_buffer_pointer = 0;177177+ s->pcm_period_pointer = 0;178178+ s->pointer_flush = true;179179+}180180+EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);169181170182static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)171183{···394376 s->pcm_period_pointer += data_blocks;395377 if (s->pcm_period_pointer >= pcm->runtime->period_size) {396378 s->pcm_period_pointer -= pcm->runtime->period_size;397397- snd_pcm_period_elapsed(pcm);379379+ s->pointer_flush = false;380380+ tasklet_hi_schedule(&s->period_tasklet);398381 }399382 }383383+}384384+385385+static void pcm_period_tasklet(unsigned long data)386386+{387387+ struct amdtp_out_stream *s = (void *)data;388388+ struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);389389+390390+ if (pcm)391391+ snd_pcm_period_elapsed(pcm);400392}401393402394static void out_packet_callback(struct fw_iso_context *context, u32 cycle,···534506EXPORT_SYMBOL(amdtp_out_stream_start);535507536508/**509509+ * amdtp_out_stream_pcm_pointer - get the PCM buffer position510510+ * @s: the AMDTP output stream that transports the PCM data511511+ *512512+ * Returns the current buffer position, in frames.513513+ */514514+unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)515515+{516516+ /* this optimization is allowed to be racy */517517+ if (s->pointer_flush)518518+ fw_iso_context_flush_completions(s->context);519519+ else520520+ s->pointer_flush = true;521521+522522+ return ACCESS_ONCE(s->pcm_buffer_pointer);523523+}524524+EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer);525525+526526+/**537527 * amdtp_out_stream_update - update the stream after a bus reset538528 * @s: the AMDTP output stream539529 */···578532 return;579533 }580534535535+ tasklet_kill(&s->period_tasklet);581536 fw_iso_context_stop(s->context);582537 fw_iso_context_destroy(s->context);583538 s->context = ERR_PTR(-1);
+5-24
sound/firewire/amdtp.h
···11#ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED22#define SOUND_FIREWIRE_AMDTP_H_INCLUDED3344+#include <linux/interrupt.h>45#include <linux/mutex.h>56#include <linux/spinlock.h>67#include "packets-buffer.h"···5655 struct iso_packets_buffer buffer;57565857 struct snd_pcm_substream *pcm;5858+ struct tasklet_struct period_tasklet;59596060 int packet_index;6161 unsigned int data_block_counter;···68666967 unsigned int pcm_buffer_pointer;7068 unsigned int pcm_period_pointer;6969+ bool pointer_flush;7170};72717372int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,···84818582void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,8683 snd_pcm_format_t format);8484+void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s);8585+unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s);8786void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s);88878988/**···128123}129124130125/**131131- * amdtp_out_stream_pcm_prepare - prepare PCM device for running132132- * @s: the AMDTP output stream133133- *134134- * This function should be called from the PCM device's .prepare callback.135135- */136136-static inline void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)137137-{138138- s->pcm_buffer_pointer = 0;139139- s->pcm_period_pointer = 0;140140-}141141-142142-/**143126 * amdtp_out_stream_pcm_trigger - start/stop playback from a PCM device144127 * @s: the AMDTP output stream145128 * @pcm: the PCM device to be started, or %NULL to stop the current device···140147 struct snd_pcm_substream *pcm)141148{142149 ACCESS_ONCE(s->pcm) = pcm;143143-}144144-145145-/**146146- * amdtp_out_stream_pcm_pointer - get the PCM buffer position147147- * @s: the AMDTP output stream that transports the PCM data148148- *149149- * Returns the current buffer position, in frames.150150- */151151-static inline unsigned long152152-amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)153153-{154154- return ACCESS_ONCE(s->pcm_buffer_pointer);155150}156151157152static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
+1-1
sound/pci/Kconfig
···228228 Say Y here to include support for sound cards based on the229229 C-Media CMI8788 (Oxygen HD Audio) chip:230230 * Asound A-8788231231- * Asus Xonar DG231231+ * Asus Xonar DG/DGX232232 * AuzenTech X-Meridian233233 * AuzenTech X-Meridian 2G234234 * Bgears b-Enspirer
···3030};31313232struct snd_usb_substream;3333+struct snd_usb_endpoint;33343435struct snd_urb_ctx {3536 struct urb *urb;3637 unsigned int buffer_size; /* size of data buffer, if data URB */3738 struct snd_usb_substream *subs;3939+ struct snd_usb_endpoint *ep;3840 int index; /* index for urb array */3941 int packets; /* number of packets per urb */4242+ int packet_size[MAX_PACKS_HS]; /* size of packets for next submission */4343+ struct list_head ready_list;4044};41454242-struct snd_urb_ops {4343- int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4444- int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4545- int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4646- int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);4646+struct snd_usb_endpoint {4747+ struct snd_usb_audio *chip;4848+4949+ int use_count;5050+ int ep_num; /* the referenced endpoint number */5151+ int type; /* SND_USB_ENDPOINT_TYPE_* */5252+ unsigned long flags;5353+5454+ void (*prepare_data_urb) (struct snd_usb_substream *subs,5555+ struct urb *urb);5656+ void (*retire_data_urb) (struct snd_usb_substream *subs,5757+ struct urb *urb);5858+5959+ struct snd_usb_substream *data_subs;6060+ struct snd_usb_endpoint *sync_master;6161+ struct snd_usb_endpoint *sync_slave;6262+6363+ struct snd_urb_ctx urb[MAX_URBS];6464+6565+ struct snd_usb_packet_info {6666+ uint32_t packet_size[MAX_PACKS_HS];6767+ int packets;6868+ } next_packet[MAX_URBS];6969+ int next_packet_read_pos, next_packet_write_pos;7070+ struct list_head ready_playback_urbs;7171+7272+ unsigned int nurbs; /* # urbs */7373+ unsigned long active_mask; /* bitmask of active urbs */7474+ unsigned long unlink_mask; /* bitmask of unlinked urbs */7575+ char *syncbuf; /* sync buffer for all sync URBs */7676+ dma_addr_t sync_dma; /* DMA address of syncbuf */7777+7878+ unsigned int pipe; /* the data i/o pipe */7979+ unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */8080+ unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */8181+ int freqshift; /* how much to shift the feedback value to get Q16.16 */8282+ unsigned int freqmax; /* maximum sampling rate, used for buffer management */8383+ unsigned int phase; /* phase accumulator */8484+ unsigned int maxpacksize; /* max packet size in bytes */8585+ unsigned int maxframesize; /* max packet size in frames */8686+ unsigned int curpacksize; /* current packet size in bytes (for capture) */8787+ unsigned int curframesize; /* current packet size in frames (for capture) */8888+ unsigned int syncmaxsize; /* sync endpoint packet size */8989+ unsigned int fill_max:1; /* fill max packet size always */9090+ unsigned int datainterval; /* log_2 of data packet interval */9191+ unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */9292+ unsigned char silence_value;9393+ unsigned int stride;9494+ int iface, alt_idx;9595+9696+ spinlock_t lock;9797+ struct list_head list;4798};489949100struct snd_usb_substream {···10857 unsigned int cur_rate; /* current rate (for hw_params callback) */10958 unsigned int period_bytes; /* current period bytes (for hw_params callback) */11059 unsigned int altset_idx; /* USB data format: index of alternate setting */111111- unsigned int datapipe; /* the data i/o pipe */112112- unsigned int syncpipe; /* 1 - async out or adaptive in */113113- unsigned int datainterval; /* log_2 of data packet interval */114114- unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */115115- unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */116116- unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */117117- int freqshift; /* how much to shift the feedback value to get Q16.16 */118118- unsigned int freqmax; /* maximum sampling rate, used for buffer management */119119- unsigned int phase; /* phase accumulator */120120- unsigned int maxpacksize; /* max packet size in bytes */121121- unsigned int maxframesize; /* max packet size in frames */122122- unsigned int curpacksize; /* current packet size in bytes (for capture) */123123- unsigned int curframesize; /* current packet size in frames (for capture) */124124- unsigned int syncmaxsize; /* sync endpoint packet size */125125- unsigned int fill_max: 1; /* fill max packet size always */12660 unsigned int txfr_quirk:1; /* allow sub-frame alignment */12761 unsigned int fmt_type; /* USB audio format type (1-3) */12862···11882 unsigned long active_mask; /* bitmask of active urbs */11983 unsigned long unlink_mask; /* bitmask of unlinked urbs */12084121121- unsigned int nurbs; /* # urbs */122122- struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */123123- struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */124124- char *syncbuf; /* sync buffer for all sync URBs */125125- dma_addr_t sync_dma; /* DMA address of syncbuf */8585+ /* data and sync endpoints for this stream */8686+ struct snd_usb_endpoint *data_endpoint;8787+ struct snd_usb_endpoint *sync_endpoint;8888+ unsigned long flags;1268912790 u64 formats; /* format bitmasks (all or'ed) */12891 unsigned int num_formats; /* number of supported audio formats (list) */···12994 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */13095 spinlock_t lock;13196132132- struct snd_urb_ops ops; /* callbacks (must be filled at init) */13397 int last_frame_number; /* stored frame number */13498 int last_delay; /* stored delay */13599};
+1034-839
sound/usb/endpoint.c
···2020#include <linux/ratelimit.h>2121#include <linux/usb.h>2222#include <linux/usb/audio.h>2323+#include <linux/slab.h>23242425#include <sound/core.h>2526#include <sound/pcm.h>2727+#include <sound/pcm_params.h>26282729#include "usbaudio.h"2830#include "helper.h"2931#include "card.h"3032#include "endpoint.h"3133#include "pcm.h"3434+3535+#define EP_FLAG_ACTIVATED 03636+#define EP_FLAG_RUNNING 13737+3838+/*3939+ * snd_usb_endpoint is a model that abstracts everything related to an4040+ * USB endpoint and its streaming.4141+ *4242+ * There are functions to activate and deactivate the streaming URBs and4343+ * optional callbacks to let the pcm logic handle the actual content of the4444+ * packets for playback and record. Thus, the bus streaming and the audio4545+ * handlers are fully decoupled.4646+ *4747+ * There are two different types of endpoints in audio applications.4848+ *4949+ * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both5050+ * inbound and outbound traffic.5151+ *5252+ * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and5353+ * expect the payload to carry Q10.14 / Q16.16 formatted sync information5454+ * (3 or 4 bytes).5555+ *5656+ * Each endpoint has to be configured prior to being used by calling5757+ * snd_usb_endpoint_set_params().5858+ *5959+ * The model incorporates a reference counting, so that multiple users6060+ * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and6161+ * only the first user will effectively start the URBs, and only the last6262+ * one to stop it will tear the URBs down again.6363+ */32643365/*3466 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)···8149}82508351/*8484- * unlink active urbs.8585- */8686-static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)8787-{8888- struct snd_usb_audio *chip = subs->stream->chip;8989- unsigned int i;9090- int async;9191-9292- subs->running = 0;9393-9494- if (!force && subs->stream->chip->shutdown) /* to be sure... */9595- return -EBADFD;9696-9797- async = !can_sleep && chip->async_unlink;9898-9999- if (!async && in_interrupt())100100- return 0;101101-102102- for (i = 0; i < subs->nurbs; i++) {103103- if (test_bit(i, &subs->active_mask)) {104104- if (!test_and_set_bit(i, &subs->unlink_mask)) {105105- struct urb *u = subs->dataurb[i].urb;106106- if (async)107107- usb_unlink_urb(u);108108- else109109- usb_kill_urb(u);110110- }111111- }112112- }113113- if (subs->syncpipe) {114114- for (i = 0; i < SYNC_URBS; i++) {115115- if (test_bit(i+16, &subs->active_mask)) {116116- if (!test_and_set_bit(i+16, &subs->unlink_mask)) {117117- struct urb *u = subs->syncurb[i].urb;118118- if (async)119119- usb_unlink_urb(u);120120- else121121- usb_kill_urb(u);122122- }123123- }124124- }125125- }126126- return 0;127127-}128128-129129-130130-/*13152 * release a urb data13253 */13354static void release_urb_ctx(struct snd_urb_ctx *u)13455{135135- if (u->urb) {136136- if (u->buffer_size)137137- usb_free_coherent(u->subs->dev, u->buffer_size,138138- u->urb->transfer_buffer,139139- u->urb->transfer_dma);140140- usb_free_urb(u->urb);141141- u->urb = NULL;142142- }143143-}144144-145145-/*146146- * wait until all urbs are processed.147147- */148148-static int wait_clear_urbs(struct snd_usb_substream *subs)149149-{150150- unsigned long end_time = jiffies + msecs_to_jiffies(1000);151151- unsigned int i;152152- int alive;153153-154154- do {155155- alive = 0;156156- for (i = 0; i < subs->nurbs; i++) {157157- if (test_bit(i, &subs->active_mask))158158- alive++;159159- }160160- if (subs->syncpipe) {161161- for (i = 0; i < SYNC_URBS; i++) {162162- if (test_bit(i + 16, &subs->active_mask))163163- alive++;164164- }165165- }166166- if (! alive)167167- break;168168- schedule_timeout_uninterruptible(1);169169- } while (time_before(jiffies, end_time));170170- if (alive)171171- snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);172172- return 0;173173-}174174-175175-/*176176- * release a substream177177- */178178-void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)179179-{180180- int i;181181-182182- /* stop urbs (to be sure) */183183- deactivate_urbs(subs, force, 1);184184- wait_clear_urbs(subs);185185-186186- for (i = 0; i < MAX_URBS; i++)187187- release_urb_ctx(&subs->dataurb[i]);188188- for (i = 0; i < SYNC_URBS; i++)189189- release_urb_ctx(&subs->syncurb[i]);190190- usb_free_coherent(subs->dev, SYNC_URBS * 4,191191- subs->syncbuf, subs->sync_dma);192192- subs->syncbuf = NULL;193193- subs->nurbs = 0;194194-}195195-196196-/*197197- * complete callback from data urb198198- */199199-static void snd_complete_urb(struct urb *urb)200200-{201201- struct snd_urb_ctx *ctx = urb->context;202202- struct snd_usb_substream *subs = ctx->subs;203203- struct snd_pcm_substream *substream = ctx->subs->pcm_substream;204204- int err = 0;205205-206206- if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||207207- !subs->running || /* can be stopped during retire callback */208208- (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||209209- (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {210210- clear_bit(ctx->index, &subs->active_mask);211211- if (err < 0) {212212- snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);213213- snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);214214- }215215- }216216-}217217-218218-219219-/*220220- * complete callback from sync urb221221- */222222-static void snd_complete_sync_urb(struct urb *urb)223223-{224224- struct snd_urb_ctx *ctx = urb->context;225225- struct snd_usb_substream *subs = ctx->subs;226226- struct snd_pcm_substream *substream = ctx->subs->pcm_substream;227227- int err = 0;228228-229229- if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||230230- !subs->running || /* can be stopped during retire callback */231231- (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||232232- (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {233233- clear_bit(ctx->index + 16, &subs->active_mask);234234- if (err < 0) {235235- snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);236236- snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);237237- }238238- }239239-}240240-241241-242242-/*243243- * initialize a substream for plaback/capture244244- */245245-int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,246246- unsigned int period_bytes,247247- unsigned int rate,248248- unsigned int frame_bits)249249-{250250- unsigned int maxsize, i;251251- int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;252252- unsigned int urb_packs, total_packs, packs_per_ms;253253- struct snd_usb_audio *chip = subs->stream->chip;254254-255255- /* calculate the frequency in 16.16 format */256256- if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)257257- subs->freqn = get_usb_full_speed_rate(rate);258258- else259259- subs->freqn = get_usb_high_speed_rate(rate);260260- subs->freqm = subs->freqn;261261- subs->freqshift = INT_MIN;262262- /* calculate max. frequency */263263- if (subs->maxpacksize) {264264- /* whatever fits into a max. size packet */265265- maxsize = subs->maxpacksize;266266- subs->freqmax = (maxsize / (frame_bits >> 3))267267- << (16 - subs->datainterval);268268- } else {269269- /* no max. packet size: just take 25% higher than nominal */270270- subs->freqmax = subs->freqn + (subs->freqn >> 2);271271- maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))272272- >> (16 - subs->datainterval);273273- }274274- subs->phase = 0;275275-276276- if (subs->fill_max)277277- subs->curpacksize = subs->maxpacksize;278278- else279279- subs->curpacksize = maxsize;280280-281281- if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)282282- packs_per_ms = 8 >> subs->datainterval;283283- else284284- packs_per_ms = 1;285285-286286- if (is_playback) {287287- urb_packs = max(chip->nrpacks, 1);288288- urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);289289- } else290290- urb_packs = 1;291291- urb_packs *= packs_per_ms;292292- if (subs->syncpipe)293293- urb_packs = min(urb_packs, 1U << subs->syncinterval);294294-295295- /* decide how many packets to be used */296296- if (is_playback) {297297- unsigned int minsize, maxpacks;298298- /* determine how small a packet can be */299299- minsize = (subs->freqn >> (16 - subs->datainterval))300300- * (frame_bits >> 3);301301- /* with sync from device, assume it can be 12% lower */302302- if (subs->syncpipe)303303- minsize -= minsize >> 3;304304- minsize = max(minsize, 1u);305305- total_packs = (period_bytes + minsize - 1) / minsize;306306- /* we need at least two URBs for queueing */307307- if (total_packs < 2) {308308- total_packs = 2;309309- } else {310310- /* and we don't want too long a queue either */311311- maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);312312- total_packs = min(total_packs, maxpacks);313313- }314314- } else {315315- while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)316316- urb_packs >>= 1;317317- total_packs = MAX_URBS * urb_packs;318318- }319319- subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;320320- if (subs->nurbs > MAX_URBS) {321321- /* too much... */322322- subs->nurbs = MAX_URBS;323323- total_packs = MAX_URBS * urb_packs;324324- } else if (subs->nurbs < 2) {325325- /* too little - we need at least two packets326326- * to ensure contiguous playback/capture327327- */328328- subs->nurbs = 2;329329- }330330-331331- /* allocate and initialize data urbs */332332- for (i = 0; i < subs->nurbs; i++) {333333- struct snd_urb_ctx *u = &subs->dataurb[i];334334- u->index = i;335335- u->subs = subs;336336- u->packets = (i + 1) * total_packs / subs->nurbs337337- - i * total_packs / subs->nurbs;338338- u->buffer_size = maxsize * u->packets;339339- if (subs->fmt_type == UAC_FORMAT_TYPE_II)340340- u->packets++; /* for transfer delimiter */341341- u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);342342- if (!u->urb)343343- goto out_of_memory;344344- u->urb->transfer_buffer =345345- usb_alloc_coherent(subs->dev, u->buffer_size,346346- GFP_KERNEL, &u->urb->transfer_dma);347347- if (!u->urb->transfer_buffer)348348- goto out_of_memory;349349- u->urb->pipe = subs->datapipe;350350- u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;351351- u->urb->interval = 1 << subs->datainterval;352352- u->urb->context = u;353353- u->urb->complete = snd_complete_urb;354354- }355355-356356- if (subs->syncpipe) {357357- /* allocate and initialize sync urbs */358358- subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,359359- GFP_KERNEL, &subs->sync_dma);360360- if (!subs->syncbuf)361361- goto out_of_memory;362362- for (i = 0; i < SYNC_URBS; i++) {363363- struct snd_urb_ctx *u = &subs->syncurb[i];364364- u->index = i;365365- u->subs = subs;366366- u->packets = 1;367367- u->urb = usb_alloc_urb(1, GFP_KERNEL);368368- if (!u->urb)369369- goto out_of_memory;370370- u->urb->transfer_buffer = subs->syncbuf + i * 4;371371- u->urb->transfer_dma = subs->sync_dma + i * 4;372372- u->urb->transfer_buffer_length = 4;373373- u->urb->pipe = subs->syncpipe;374374- u->urb->transfer_flags = URB_ISO_ASAP |375375- URB_NO_TRANSFER_DMA_MAP;376376- u->urb->number_of_packets = 1;377377- u->urb->interval = 1 << subs->syncinterval;378378- u->urb->context = u;379379- u->urb->complete = snd_complete_sync_urb;380380- }381381- }382382- return 0;383383-384384-out_of_memory:385385- snd_usb_release_substream_urbs(subs, 0);386386- return -ENOMEM;387387-}388388-389389-/*390390- * prepare urb for full speed capture sync pipe391391- *392392- * fill the length and offset of each urb descriptor.393393- * the fixed 10.14 frequency is passed through the pipe.394394- */395395-static int prepare_capture_sync_urb(struct snd_usb_substream *subs,396396- struct snd_pcm_runtime *runtime,397397- struct urb *urb)398398-{399399- unsigned char *cp = urb->transfer_buffer;400400- struct snd_urb_ctx *ctx = urb->context;401401-402402- urb->dev = ctx->subs->dev; /* we need to set this at each time */403403- urb->iso_frame_desc[0].length = 3;404404- urb->iso_frame_desc[0].offset = 0;405405- cp[0] = subs->freqn >> 2;406406- cp[1] = subs->freqn >> 10;407407- cp[2] = subs->freqn >> 18;408408- return 0;409409-}410410-411411-/*412412- * prepare urb for high speed capture sync pipe413413- *414414- * fill the length and offset of each urb descriptor.415415- * the fixed 12.13 frequency is passed as 16.16 through the pipe.416416- */417417-static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,418418- struct snd_pcm_runtime *runtime,419419- struct urb *urb)420420-{421421- unsigned char *cp = urb->transfer_buffer;422422- struct snd_urb_ctx *ctx = urb->context;423423-424424- urb->dev = ctx->subs->dev; /* we need to set this at each time */425425- urb->iso_frame_desc[0].length = 4;426426- urb->iso_frame_desc[0].offset = 0;427427- cp[0] = subs->freqn;428428- cp[1] = subs->freqn >> 8;429429- cp[2] = subs->freqn >> 16;430430- cp[3] = subs->freqn >> 24;431431- return 0;432432-}433433-434434-/*435435- * process after capture sync complete436436- * - nothing to do437437- */438438-static int retire_capture_sync_urb(struct snd_usb_substream *subs,439439- struct snd_pcm_runtime *runtime,440440- struct urb *urb)441441-{442442- return 0;443443-}444444-445445-/*446446- * prepare urb for capture data pipe447447- *448448- * fill the offset and length of each descriptor.449449- *450450- * we use a temporary buffer to write the captured data.451451- * since the length of written data is determined by host, we cannot452452- * write onto the pcm buffer directly... the data is thus copied453453- * later at complete callback to the global buffer.454454- */455455-static int prepare_capture_urb(struct snd_usb_substream *subs,456456- struct snd_pcm_runtime *runtime,457457- struct urb *urb)458458-{459459- int i, offs;460460- struct snd_urb_ctx *ctx = urb->context;461461-462462- offs = 0;463463- urb->dev = ctx->subs->dev; /* we need to set this at each time */464464- for (i = 0; i < ctx->packets; i++) {465465- urb->iso_frame_desc[i].offset = offs;466466- urb->iso_frame_desc[i].length = subs->curpacksize;467467- offs += subs->curpacksize;468468- }469469- urb->transfer_buffer_length = offs;470470- urb->number_of_packets = ctx->packets;471471- return 0;472472-}473473-474474-/*475475- * process after capture complete476476- *477477- * copy the data from each desctiptor to the pcm buffer, and478478- * update the current position.479479- */480480-static int retire_capture_urb(struct snd_usb_substream *subs,481481- struct snd_pcm_runtime *runtime,482482- struct urb *urb)483483-{484484- unsigned long flags;485485- unsigned char *cp;486486- int i;487487- unsigned int stride, frames, bytes, oldptr;488488- int period_elapsed = 0;489489-490490- stride = runtime->frame_bits >> 3;491491-492492- for (i = 0; i < urb->number_of_packets; i++) {493493- cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;494494- if (urb->iso_frame_desc[i].status && printk_ratelimit()) {495495- snd_printdd("frame %d active: %d\n", i, urb->iso_frame_desc[i].status);496496- // continue;497497- }498498- bytes = urb->iso_frame_desc[i].actual_length;499499- frames = bytes / stride;500500- if (!subs->txfr_quirk)501501- bytes = frames * stride;502502- if (bytes % (runtime->sample_bits >> 3) != 0) {503503-#ifdef CONFIG_SND_DEBUG_VERBOSE504504- int oldbytes = bytes;505505-#endif506506- bytes = frames * stride;507507- snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",508508- oldbytes, bytes);509509- }510510- /* update the current pointer */511511- spin_lock_irqsave(&subs->lock, flags);512512- oldptr = subs->hwptr_done;513513- subs->hwptr_done += bytes;514514- if (subs->hwptr_done >= runtime->buffer_size * stride)515515- subs->hwptr_done -= runtime->buffer_size * stride;516516- frames = (bytes + (oldptr % stride)) / stride;517517- subs->transfer_done += frames;518518- if (subs->transfer_done >= runtime->period_size) {519519- subs->transfer_done -= runtime->period_size;520520- period_elapsed = 1;521521- }522522- spin_unlock_irqrestore(&subs->lock, flags);523523- /* copy a data chunk */524524- if (oldptr + bytes > runtime->buffer_size * stride) {525525- unsigned int bytes1 =526526- runtime->buffer_size * stride - oldptr;527527- memcpy(runtime->dma_area + oldptr, cp, bytes1);528528- memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);529529- } else {530530- memcpy(runtime->dma_area + oldptr, cp, bytes);531531- }532532- }533533- if (period_elapsed)534534- snd_pcm_period_elapsed(subs->pcm_substream);535535- return 0;536536-}537537-538538-/*539539- * Process after capture complete when paused. Nothing to do.540540- */541541-static int retire_paused_capture_urb(struct snd_usb_substream *subs,542542- struct snd_pcm_runtime *runtime,543543- struct urb *urb)544544-{545545- return 0;546546-}547547-548548-549549-/*550550- * prepare urb for playback sync pipe551551- *552552- * set up the offset and length to receive the current frequency.553553- */554554-static int prepare_playback_sync_urb(struct snd_usb_substream *subs,555555- struct snd_pcm_runtime *runtime,556556- struct urb *urb)557557-{558558- struct snd_urb_ctx *ctx = urb->context;559559-560560- urb->dev = ctx->subs->dev; /* we need to set this at each time */561561- urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize);562562- urb->iso_frame_desc[0].offset = 0;563563- return 0;564564-}565565-566566-/*567567- * process after playback sync complete568568- *569569- * Full speed devices report feedback values in 10.14 format as samples per570570- * frame, high speed devices in 16.16 format as samples per microframe.571571- * Because the Audio Class 1 spec was written before USB 2.0, many high speed572572- * devices use a wrong interpretation, some others use an entirely different573573- * format. Therefore, we cannot predict what format any particular device uses574574- * and must detect it automatically.575575- */576576-static int retire_playback_sync_urb(struct snd_usb_substream *subs,577577- struct snd_pcm_runtime *runtime,578578- struct urb *urb)579579-{580580- unsigned int f;581581- int shift;582582- unsigned long flags;583583-584584- if (urb->iso_frame_desc[0].status != 0 ||585585- urb->iso_frame_desc[0].actual_length < 3)586586- return 0;587587-588588- f = le32_to_cpup(urb->transfer_buffer);589589- if (urb->iso_frame_desc[0].actual_length == 3)590590- f &= 0x00ffffff;591591- else592592- f &= 0x0fffffff;593593- if (f == 0)594594- return 0;595595-596596- if (unlikely(subs->freqshift == INT_MIN)) {597597- /*598598- * The first time we see a feedback value, determine its format599599- * by shifting it left or right until it matches the nominal600600- * frequency value. This assumes that the feedback does not601601- * differ from the nominal value more than +50% or -25%.602602- */603603- shift = 0;604604- while (f < subs->freqn - subs->freqn / 4) {605605- f <<= 1;606606- shift++;607607- }608608- while (f > subs->freqn + subs->freqn / 2) {609609- f >>= 1;610610- shift--;611611- }612612- subs->freqshift = shift;613613- }614614- else if (subs->freqshift >= 0)615615- f <<= subs->freqshift;616616- else617617- f >>= -subs->freqshift;618618-619619- if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) {620620- /*621621- * If the frequency looks valid, set it.622622- * This value is referred to in prepare_playback_urb().623623- */624624- spin_lock_irqsave(&subs->lock, flags);625625- subs->freqm = f;626626- spin_unlock_irqrestore(&subs->lock, flags);627627- } else {628628- /*629629- * Out of range; maybe the shift value is wrong.630630- * Reset it so that we autodetect again the next time.631631- */632632- subs->freqshift = INT_MIN;633633- }634634-635635- return 0;636636-}637637-638638-/* determine the number of frames in the next packet */639639-static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)640640-{641641- if (subs->fill_max)642642- return subs->maxframesize;643643- else {644644- subs->phase = (subs->phase & 0xffff)645645- + (subs->freqm << subs->datainterval);646646- return min(subs->phase >> 16, subs->maxframesize);647647- }648648-}649649-650650-/*651651- * Prepare urb for streaming before playback starts or when paused.652652- *653653- * We don't have any data, so we send silence.654654- */655655-static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,656656- struct snd_pcm_runtime *runtime,657657- struct urb *urb)658658-{659659- unsigned int i, offs, counts;660660- struct snd_urb_ctx *ctx = urb->context;661661- int stride = runtime->frame_bits >> 3;662662-663663- offs = 0;664664- urb->dev = ctx->subs->dev;665665- for (i = 0; i < ctx->packets; ++i) {666666- counts = snd_usb_audio_next_packet_size(subs);667667- urb->iso_frame_desc[i].offset = offs * stride;668668- urb->iso_frame_desc[i].length = counts * stride;669669- offs += counts;670670- }671671- urb->number_of_packets = ctx->packets;672672- urb->transfer_buffer_length = offs * stride;673673- memset(urb->transfer_buffer,674674- runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,675675- offs * stride);676676- return 0;677677-}678678-679679-/*680680- * prepare urb for playback data pipe681681- *682682- * Since a URB can handle only a single linear buffer, we must use double683683- * buffering when the data to be transferred overflows the buffer boundary.684684- * To avoid inconsistencies when updating hwptr_done, we use double buffering685685- * for all URBs.686686- */687687-static int prepare_playback_urb(struct snd_usb_substream *subs,688688- struct snd_pcm_runtime *runtime,689689- struct urb *urb)690690-{691691- int i, stride;692692- unsigned int counts, frames, bytes;693693- unsigned long flags;694694- int period_elapsed = 0;695695- struct snd_urb_ctx *ctx = urb->context;696696-697697- stride = runtime->frame_bits >> 3;698698-699699- frames = 0;700700- urb->dev = ctx->subs->dev; /* we need to set this at each time */701701- urb->number_of_packets = 0;702702- spin_lock_irqsave(&subs->lock, flags);703703- for (i = 0; i < ctx->packets; i++) {704704- counts = snd_usb_audio_next_packet_size(subs);705705- /* set up descriptor */706706- urb->iso_frame_desc[i].offset = frames * stride;707707- urb->iso_frame_desc[i].length = counts * stride;708708- frames += counts;709709- urb->number_of_packets++;710710- subs->transfer_done += counts;711711- if (subs->transfer_done >= runtime->period_size) {712712- subs->transfer_done -= runtime->period_size;713713- period_elapsed = 1;714714- if (subs->fmt_type == UAC_FORMAT_TYPE_II) {715715- if (subs->transfer_done > 0) {716716- /* FIXME: fill-max mode is not717717- * supported yet */718718- frames -= subs->transfer_done;719719- counts -= subs->transfer_done;720720- urb->iso_frame_desc[i].length =721721- counts * stride;722722- subs->transfer_done = 0;723723- }724724- i++;725725- if (i < ctx->packets) {726726- /* add a transfer delimiter */727727- urb->iso_frame_desc[i].offset =728728- frames * stride;729729- urb->iso_frame_desc[i].length = 0;730730- urb->number_of_packets++;731731- }732732- break;733733- }734734- }735735- if (period_elapsed) /* finish at the period boundary */736736- break;737737- }738738- bytes = frames * stride;739739- if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {740740- /* err, the transferred area goes over buffer boundary. */741741- unsigned int bytes1 =742742- runtime->buffer_size * stride - subs->hwptr_done;743743- memcpy(urb->transfer_buffer,744744- runtime->dma_area + subs->hwptr_done, bytes1);745745- memcpy(urb->transfer_buffer + bytes1,746746- runtime->dma_area, bytes - bytes1);747747- } else {748748- memcpy(urb->transfer_buffer,749749- runtime->dma_area + subs->hwptr_done, bytes);750750- }751751- subs->hwptr_done += bytes;752752- if (subs->hwptr_done >= runtime->buffer_size * stride)753753- subs->hwptr_done -= runtime->buffer_size * stride;754754-755755- /* update delay with exact number of samples queued */756756- runtime->delay = subs->last_delay;757757- runtime->delay += frames;758758- subs->last_delay = runtime->delay;759759-760760- /* realign last_frame_number */761761- subs->last_frame_number = usb_get_current_frame_number(subs->dev);762762- subs->last_frame_number &= 0xFF; /* keep 8 LSBs */763763-764764- spin_unlock_irqrestore(&subs->lock, flags);765765- urb->transfer_buffer_length = bytes;766766- if (period_elapsed)767767- snd_pcm_period_elapsed(subs->pcm_substream);768768- return 0;769769-}770770-771771-/*772772- * process after playback data complete773773- * - decrease the delay count again774774- */775775-static int retire_playback_urb(struct snd_usb_substream *subs,776776- struct snd_pcm_runtime *runtime,777777- struct urb *urb)778778-{779779- unsigned long flags;780780- int stride = runtime->frame_bits >> 3;781781- int processed = urb->transfer_buffer_length / stride;782782- int est_delay;783783-784784- spin_lock_irqsave(&subs->lock, flags);785785-786786- est_delay = snd_usb_pcm_delay(subs, runtime->rate);787787- /* update delay with exact number of samples played */788788- if (processed > subs->last_delay)789789- subs->last_delay = 0;790790- else791791- subs->last_delay -= processed;792792- runtime->delay = subs->last_delay;793793-794794- /*795795- * Report when delay estimate is off by more than 2ms.796796- * The error should be lower than 2ms since the estimate relies797797- * on two reads of a counter updated every ms.798798- */799799- if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)800800- snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",801801- est_delay, subs->last_delay);802802-803803- spin_unlock_irqrestore(&subs->lock, flags);804804- return 0;5656+ if (u->buffer_size)5757+ usb_free_coherent(u->ep->chip->dev, u->buffer_size,5858+ u->urb->transfer_buffer,5959+ u->urb->transfer_dma);6060+ usb_free_urb(u->urb);6161+ u->urb = NULL;80562}8066380764static const char *usb_error_string(int err)···118797 }119798}120799121121-/*122122- * set up and start data/sync urbs800800+/**801801+ * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type802802+ *803803+ * @ep: The snd_usb_endpoint804804+ *805805+ * Determine whether an endpoint is driven by an implicit feedback806806+ * data endpoint source.123807 */124124-static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)808808+int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep)125809{126126- unsigned int i;810810+ return ep->sync_master &&811811+ ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&812812+ ep->type == SND_USB_ENDPOINT_TYPE_DATA &&813813+ usb_pipeout(ep->pipe);814814+}815815+816816+/*817817+ * For streaming based on information derived from sync endpoints,818818+ * prepare_outbound_urb_sizes() will call next_packet_size() to819819+ * determine the number of samples to be sent in the next packet.820820+ *821821+ * For implicit feedback, next_packet_size() is unused.822822+ */823823+static int next_packet_size(struct snd_usb_endpoint *ep)824824+{825825+ unsigned long flags;826826+ int ret;827827+828828+ if (ep->fill_max)829829+ return ep->maxframesize;830830+831831+ spin_lock_irqsave(&ep->lock, flags);832832+ ep->phase = (ep->phase & 0xffff)833833+ + (ep->freqm << ep->datainterval);834834+ ret = min(ep->phase >> 16, ep->maxframesize);835835+ spin_unlock_irqrestore(&ep->lock, flags);836836+837837+ return ret;838838+}839839+840840+static void retire_outbound_urb(struct snd_usb_endpoint *ep,841841+ struct snd_urb_ctx *urb_ctx)842842+{843843+ if (ep->retire_data_urb)844844+ ep->retire_data_urb(ep->data_subs, urb_ctx->urb);845845+}846846+847847+static void retire_inbound_urb(struct snd_usb_endpoint *ep,848848+ struct snd_urb_ctx *urb_ctx)849849+{850850+ struct urb *urb = urb_ctx->urb;851851+852852+ if (ep->sync_slave)853853+ snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);854854+855855+ if (ep->retire_data_urb)856856+ ep->retire_data_urb(ep->data_subs, urb);857857+}858858+859859+static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep,860860+ struct snd_urb_ctx *ctx)861861+{862862+ int i;863863+864864+ for (i = 0; i < ctx->packets; ++i)865865+ ctx->packet_size[i] = next_packet_size(ep);866866+}867867+868868+/*869869+ * Prepare a PLAYBACK urb for submission to the bus.870870+ */871871+static void prepare_outbound_urb(struct snd_usb_endpoint *ep,872872+ struct snd_urb_ctx *ctx)873873+{874874+ int i;875875+ struct urb *urb = ctx->urb;876876+ unsigned char *cp = urb->transfer_buffer;877877+878878+ urb->dev = ep->chip->dev; /* we need to set this at each time */879879+880880+ switch (ep->type) {881881+ case SND_USB_ENDPOINT_TYPE_DATA:882882+ if (ep->prepare_data_urb) {883883+ ep->prepare_data_urb(ep->data_subs, urb);884884+ } else {885885+ /* no data provider, so send silence */886886+ unsigned int offs = 0;887887+ for (i = 0; i < ctx->packets; ++i) {888888+ int counts = ctx->packet_size[i];889889+ urb->iso_frame_desc[i].offset = offs * ep->stride;890890+ urb->iso_frame_desc[i].length = counts * ep->stride;891891+ offs += counts;892892+ }893893+894894+ urb->number_of_packets = ctx->packets;895895+ urb->transfer_buffer_length = offs * ep->stride;896896+ memset(urb->transfer_buffer, ep->silence_value,897897+ offs * ep->stride);898898+ }899899+ break;900900+901901+ case SND_USB_ENDPOINT_TYPE_SYNC:902902+ if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {903903+ /*904904+ * fill the length and offset of each urb descriptor.905905+ * the fixed 12.13 frequency is passed as 16.16 through the pipe.906906+ */907907+ urb->iso_frame_desc[0].length = 4;908908+ urb->iso_frame_desc[0].offset = 0;909909+ cp[0] = ep->freqn;910910+ cp[1] = ep->freqn >> 8;911911+ cp[2] = ep->freqn >> 16;912912+ cp[3] = ep->freqn >> 24;913913+ } else {914914+ /*915915+ * fill the length and offset of each urb descriptor.916916+ * the fixed 10.14 frequency is passed through the pipe.917917+ */918918+ urb->iso_frame_desc[0].length = 3;919919+ urb->iso_frame_desc[0].offset = 0;920920+ cp[0] = ep->freqn >> 2;921921+ cp[1] = ep->freqn >> 10;922922+ cp[2] = ep->freqn >> 18;923923+ }924924+925925+ break;926926+ }927927+}928928+929929+/*930930+ * Prepare a CAPTURE or SYNC urb for submission to the bus.931931+ */932932+static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,933933+ struct snd_urb_ctx *urb_ctx)934934+{935935+ int i, offs;936936+ struct urb *urb = urb_ctx->urb;937937+938938+ urb->dev = ep->chip->dev; /* we need to set this at each time */939939+940940+ switch (ep->type) {941941+ case SND_USB_ENDPOINT_TYPE_DATA:942942+ offs = 0;943943+ for (i = 0; i < urb_ctx->packets; i++) {944944+ urb->iso_frame_desc[i].offset = offs;945945+ urb->iso_frame_desc[i].length = ep->curpacksize;946946+ offs += ep->curpacksize;947947+ }948948+949949+ urb->transfer_buffer_length = offs;950950+ urb->number_of_packets = urb_ctx->packets;951951+ break;952952+953953+ case SND_USB_ENDPOINT_TYPE_SYNC:954954+ urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);955955+ urb->iso_frame_desc[0].offset = 0;956956+ break;957957+ }958958+}959959+960960+/*961961+ * Send output urbs that have been prepared previously. URBs are dequeued962962+ * from ep->ready_playback_urbs and in case there there aren't any available963963+ * or there are no packets that have been prepared, this function does964964+ * nothing.965965+ *966966+ * The reason why the functionality of sending and preparing URBs is separated967967+ * is that host controllers don't guarantee the order in which they return968968+ * inbound and outbound packets to their submitters.969969+ *970970+ * This function is only used for implicit feedback endpoints. For endpoints971971+ * driven by dedicated sync endpoints, URBs are immediately re-submitted972972+ * from their completion handler.973973+ */974974+static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)975975+{976976+ while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {977977+978978+ unsigned long flags;979979+ struct snd_usb_packet_info *uninitialized_var(packet);980980+ struct snd_urb_ctx *ctx = NULL;981981+ struct urb *urb;982982+ int err, i;983983+984984+ spin_lock_irqsave(&ep->lock, flags);985985+ if (ep->next_packet_read_pos != ep->next_packet_write_pos) {986986+ packet = ep->next_packet + ep->next_packet_read_pos;987987+ ep->next_packet_read_pos++;988988+ ep->next_packet_read_pos %= MAX_URBS;989989+990990+ /* take URB out of FIFO */991991+ if (!list_empty(&ep->ready_playback_urbs))992992+ ctx = list_first_entry(&ep->ready_playback_urbs,993993+ struct snd_urb_ctx, ready_list);994994+ }995995+ spin_unlock_irqrestore(&ep->lock, flags);996996+997997+ if (ctx == NULL)998998+ return;999999+10001000+ list_del_init(&ctx->ready_list);10011001+ urb = ctx->urb;10021002+10031003+ /* copy over the length information */10041004+ for (i = 0; i < packet->packets; i++)10051005+ ctx->packet_size[i] = packet->packet_size[i];10061006+10071007+ /* call the data handler to fill in playback data */10081008+ prepare_outbound_urb(ep, ctx);10091009+10101010+ err = usb_submit_urb(ctx->urb, GFP_ATOMIC);10111011+ if (err < 0)10121012+ snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n",10131013+ ctx->index, err, ctx->urb);10141014+ else10151015+ set_bit(ctx->index, &ep->active_mask);10161016+ }10171017+}10181018+10191019+/*10201020+ * complete callback for urbs10211021+ */10221022+static void snd_complete_urb(struct urb *urb)10231023+{10241024+ struct snd_urb_ctx *ctx = urb->context;10251025+ struct snd_usb_endpoint *ep = ctx->ep;1271026 int err;1281027129129- if (subs->stream->chip->shutdown)130130- return -EBADFD;10281028+ if (unlikely(urb->status == -ENOENT || /* unlinked */10291029+ urb->status == -ENODEV || /* device removed */10301030+ urb->status == -ECONNRESET || /* unlinked */10311031+ urb->status == -ESHUTDOWN || /* device disabled */10321032+ ep->chip->shutdown)) /* device disconnected */10331033+ goto exit_clear;1311034132132- for (i = 0; i < subs->nurbs; i++) {133133- if (snd_BUG_ON(!subs->dataurb[i].urb))134134- return -EINVAL;135135- if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {136136- snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);137137- goto __error;10351035+ if (usb_pipeout(ep->pipe)) {10361036+ retire_outbound_urb(ep, ctx);10371037+ /* can be stopped during retire callback */10381038+ if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))10391039+ goto exit_clear;10401040+10411041+ if (snd_usb_endpoint_implict_feedback_sink(ep)) {10421042+ unsigned long flags;10431043+10441044+ spin_lock_irqsave(&ep->lock, flags);10451045+ list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);10461046+ spin_unlock_irqrestore(&ep->lock, flags);10471047+ queue_pending_output_urbs(ep);10481048+10491049+ goto exit_clear;10501050+ }10511051+10521052+ prepare_outbound_urb_sizes(ep, ctx);10531053+ prepare_outbound_urb(ep, ctx);10541054+ } else {10551055+ retire_inbound_urb(ep, ctx);10561056+ /* can be stopped during retire callback */10571057+ if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))10581058+ goto exit_clear;10591059+10601060+ prepare_inbound_urb(ep, ctx);10611061+ }10621062+10631063+ err = usb_submit_urb(urb, GFP_ATOMIC);10641064+ if (err == 0)10651065+ return;10661066+10671067+ snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err);10681068+ //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);10691069+10701070+exit_clear:10711071+ clear_bit(ctx->index, &ep->active_mask);10721072+}10731073+10741074+/**10751075+ * snd_usb_add_endpoint: Add an endpoint to an USB audio chip10761076+ *10771077+ * @chip: The chip10781078+ * @alts: The USB host interface10791079+ * @ep_num: The number of the endpoint to use10801080+ * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE10811081+ * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC10821082+ *10831083+ * If the requested endpoint has not been added to the given chip before,10841084+ * a new instance is created. Otherwise, a pointer to the previoulsy10851085+ * created instance is returned. In case of any error, NULL is returned.10861086+ *10871087+ * New endpoints will be added to chip->ep_list and must be freed by10881088+ * calling snd_usb_endpoint_free().10891089+ */10901090+struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,10911091+ struct usb_host_interface *alts,10921092+ int ep_num, int direction, int type)10931093+{10941094+ struct list_head *p;10951095+ struct snd_usb_endpoint *ep;10961096+ int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK;10971097+10981098+ mutex_lock(&chip->mutex);10991099+11001100+ list_for_each(p, &chip->ep_list) {11011101+ ep = list_entry(p, struct snd_usb_endpoint, list);11021102+ if (ep->ep_num == ep_num &&11031103+ ep->iface == alts->desc.bInterfaceNumber &&11041104+ ep->alt_idx == alts->desc.bAlternateSetting) {11051105+ snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n",11061106+ ep_num, ep->iface, ep->alt_idx, ep);11071107+ goto __exit_unlock;1381108 }1391109 }140140- if (subs->syncpipe) {141141- for (i = 0; i < SYNC_URBS; i++) {142142- if (snd_BUG_ON(!subs->syncurb[i].urb))143143- return -EINVAL;144144- if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {145145- snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);146146- goto __error;11101110+11111111+ snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n",11121112+ is_playback ? "playback" : "capture",11131113+ type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",11141114+ ep_num);11151115+11161116+ /* select the alt setting once so the endpoints become valid */11171117+ ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber,11181118+ alts->desc.bAlternateSetting);11191119+ if (ret < 0) {11201120+ snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n",11211121+ __func__, ret);11221122+ ep = NULL;11231123+ goto __exit_unlock;11241124+ }11251125+11261126+ ep = kzalloc(sizeof(*ep), GFP_KERNEL);11271127+ if (!ep)11281128+ goto __exit_unlock;11291129+11301130+ ep->chip = chip;11311131+ spin_lock_init(&ep->lock);11321132+ ep->type = type;11331133+ ep->ep_num = ep_num;11341134+ ep->iface = alts->desc.bInterfaceNumber;11351135+ ep->alt_idx = alts->desc.bAlternateSetting;11361136+ INIT_LIST_HEAD(&ep->ready_playback_urbs);11371137+ ep_num &= USB_ENDPOINT_NUMBER_MASK;11381138+11391139+ if (is_playback)11401140+ ep->pipe = usb_sndisocpipe(chip->dev, ep_num);11411141+ else11421142+ ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);11431143+11441144+ if (type == SND_USB_ENDPOINT_TYPE_SYNC) {11451145+ if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&11461146+ get_endpoint(alts, 1)->bRefresh >= 1 &&11471147+ get_endpoint(alts, 1)->bRefresh <= 9)11481148+ ep->syncinterval = get_endpoint(alts, 1)->bRefresh;11491149+ else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)11501150+ ep->syncinterval = 1;11511151+ else if (get_endpoint(alts, 1)->bInterval >= 1 &&11521152+ get_endpoint(alts, 1)->bInterval <= 16)11531153+ ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;11541154+ else11551155+ ep->syncinterval = 3;11561156+11571157+ ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);11581158+ }11591159+11601160+ list_add_tail(&ep->list, &chip->ep_list);11611161+11621162+__exit_unlock:11631163+ mutex_unlock(&chip->mutex);11641164+11651165+ return ep;11661166+}11671167+11681168+/*11691169+ * wait until all urbs are processed.11701170+ */11711171+static int wait_clear_urbs(struct snd_usb_endpoint *ep)11721172+{11731173+ unsigned long end_time = jiffies + msecs_to_jiffies(1000);11741174+ unsigned int i;11751175+ int alive;11761176+11771177+ do {11781178+ alive = 0;11791179+ for (i = 0; i < ep->nurbs; i++)11801180+ if (test_bit(i, &ep->active_mask))11811181+ alive++;11821182+11831183+ if (!alive)11841184+ break;11851185+11861186+ schedule_timeout_uninterruptible(1);11871187+ } while (time_before(jiffies, end_time));11881188+11891189+ if (alive)11901190+ snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n",11911191+ alive, ep->ep_num);11921192+11931193+ return 0;11941194+}11951195+11961196+/*11971197+ * unlink active urbs.11981198+ */11991199+static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep)12001200+{12011201+ unsigned int i;12021202+ int async;12031203+12041204+ if (!force && ep->chip->shutdown) /* to be sure... */12051205+ return -EBADFD;12061206+12071207+ async = !can_sleep && ep->chip->async_unlink;12081208+12091209+ clear_bit(EP_FLAG_RUNNING, &ep->flags);12101210+12111211+ INIT_LIST_HEAD(&ep->ready_playback_urbs);12121212+ ep->next_packet_read_pos = 0;12131213+ ep->next_packet_write_pos = 0;12141214+12151215+ if (!async && in_interrupt())12161216+ return 0;12171217+12181218+ for (i = 0; i < ep->nurbs; i++) {12191219+ if (test_bit(i, &ep->active_mask)) {12201220+ if (!test_and_set_bit(i, &ep->unlink_mask)) {12211221+ struct urb *u = ep->urb[i].urb;12221222+ if (async)12231223+ usb_unlink_urb(u);12241224+ else12251225+ usb_kill_urb(u);1471226 }1481227 }1491228 }1501229151151- subs->active_mask = 0;152152- subs->unlink_mask = 0;153153- subs->running = 1;154154- for (i = 0; i < subs->nurbs; i++) {155155- err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);12301230+ return 0;12311231+}12321232+12331233+/*12341234+ * release an endpoint's urbs12351235+ */12361236+static void release_urbs(struct snd_usb_endpoint *ep, int force)12371237+{12381238+ int i;12391239+12401240+ /* route incoming urbs to nirvana */12411241+ ep->retire_data_urb = NULL;12421242+ ep->prepare_data_urb = NULL;12431243+12441244+ /* stop urbs */12451245+ deactivate_urbs(ep, force, 1);12461246+ wait_clear_urbs(ep);12471247+12481248+ for (i = 0; i < ep->nurbs; i++)12491249+ release_urb_ctx(&ep->urb[i]);12501250+12511251+ if (ep->syncbuf)12521252+ usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,12531253+ ep->syncbuf, ep->sync_dma);12541254+12551255+ ep->syncbuf = NULL;12561256+ ep->nurbs = 0;12571257+}12581258+12591259+/*12601260+ * configure a data endpoint12611261+ */12621262+static int data_ep_set_params(struct snd_usb_endpoint *ep,12631263+ struct snd_pcm_hw_params *hw_params,12641264+ struct audioformat *fmt,12651265+ struct snd_usb_endpoint *sync_ep)12661266+{12671267+ unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms;12681268+ int period_bytes = params_period_bytes(hw_params);12691269+ int format = params_format(hw_params);12701270+ int is_playback = usb_pipeout(ep->pipe);12711271+ int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) *12721272+ params_channels(hw_params);12731273+12741274+ ep->datainterval = fmt->datainterval;12751275+ ep->stride = frame_bits >> 3;12761276+ ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0;12771277+12781278+ /* calculate max. frequency */12791279+ if (ep->maxpacksize) {12801280+ /* whatever fits into a max. size packet */12811281+ maxsize = ep->maxpacksize;12821282+ ep->freqmax = (maxsize / (frame_bits >> 3))12831283+ << (16 - ep->datainterval);12841284+ } else {12851285+ /* no max. packet size: just take 25% higher than nominal */12861286+ ep->freqmax = ep->freqn + (ep->freqn >> 2);12871287+ maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3))12881288+ >> (16 - ep->datainterval);12891289+ }12901290+12911291+ if (ep->fill_max)12921292+ ep->curpacksize = ep->maxpacksize;12931293+ else12941294+ ep->curpacksize = maxsize;12951295+12961296+ if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL)12971297+ packs_per_ms = 8 >> ep->datainterval;12981298+ else12991299+ packs_per_ms = 1;13001300+13011301+ if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) {13021302+ urb_packs = max(ep->chip->nrpacks, 1);13031303+ urb_packs = min(urb_packs, (unsigned int) MAX_PACKS);13041304+ } else {13051305+ urb_packs = 1;13061306+ }13071307+13081308+ urb_packs *= packs_per_ms;13091309+13101310+ if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep))13111311+ urb_packs = min(urb_packs, 1U << sync_ep->syncinterval);13121312+13131313+ /* decide how many packets to be used */13141314+ if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) {13151315+ unsigned int minsize, maxpacks;13161316+ /* determine how small a packet can be */13171317+ minsize = (ep->freqn >> (16 - ep->datainterval))13181318+ * (frame_bits >> 3);13191319+ /* with sync from device, assume it can be 12% lower */13201320+ if (sync_ep)13211321+ minsize -= minsize >> 3;13221322+ minsize = max(minsize, 1u);13231323+ total_packs = (period_bytes + minsize - 1) / minsize;13241324+ /* we need at least two URBs for queueing */13251325+ if (total_packs < 2) {13261326+ total_packs = 2;13271327+ } else {13281328+ /* and we don't want too long a queue either */13291329+ maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);13301330+ total_packs = min(total_packs, maxpacks);13311331+ }13321332+ } else {13331333+ while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)13341334+ urb_packs >>= 1;13351335+ total_packs = MAX_URBS * urb_packs;13361336+ }13371337+13381338+ ep->nurbs = (total_packs + urb_packs - 1) / urb_packs;13391339+ if (ep->nurbs > MAX_URBS) {13401340+ /* too much... */13411341+ ep->nurbs = MAX_URBS;13421342+ total_packs = MAX_URBS * urb_packs;13431343+ } else if (ep->nurbs < 2) {13441344+ /* too little - we need at least two packets13451345+ * to ensure contiguous playback/capture13461346+ */13471347+ ep->nurbs = 2;13481348+ }13491349+13501350+ /* allocate and initialize data urbs */13511351+ for (i = 0; i < ep->nurbs; i++) {13521352+ struct snd_urb_ctx *u = &ep->urb[i];13531353+ u->index = i;13541354+ u->ep = ep;13551355+ u->packets = (i + 1) * total_packs / ep->nurbs13561356+ - i * total_packs / ep->nurbs;13571357+ u->buffer_size = maxsize * u->packets;13581358+13591359+ if (fmt->fmt_type == UAC_FORMAT_TYPE_II)13601360+ u->packets++; /* for transfer delimiter */13611361+ u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);13621362+ if (!u->urb)13631363+ goto out_of_memory;13641364+13651365+ u->urb->transfer_buffer =13661366+ usb_alloc_coherent(ep->chip->dev, u->buffer_size,13671367+ GFP_KERNEL, &u->urb->transfer_dma);13681368+ if (!u->urb->transfer_buffer)13691369+ goto out_of_memory;13701370+ u->urb->pipe = ep->pipe;13711371+ u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;13721372+ u->urb->interval = 1 << ep->datainterval;13731373+ u->urb->context = u;13741374+ u->urb->complete = snd_complete_urb;13751375+ INIT_LIST_HEAD(&u->ready_list);13761376+ }13771377+13781378+ return 0;13791379+13801380+out_of_memory:13811381+ release_urbs(ep, 0);13821382+ return -ENOMEM;13831383+}13841384+13851385+/*13861386+ * configure a sync endpoint13871387+ */13881388+static int sync_ep_set_params(struct snd_usb_endpoint *ep,13891389+ struct snd_pcm_hw_params *hw_params,13901390+ struct audioformat *fmt)13911391+{13921392+ int i;13931393+13941394+ ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,13951395+ GFP_KERNEL, &ep->sync_dma);13961396+ if (!ep->syncbuf)13971397+ return -ENOMEM;13981398+13991399+ for (i = 0; i < SYNC_URBS; i++) {14001400+ struct snd_urb_ctx *u = &ep->urb[i];14011401+ u->index = i;14021402+ u->ep = ep;14031403+ u->packets = 1;14041404+ u->urb = usb_alloc_urb(1, GFP_KERNEL);14051405+ if (!u->urb)14061406+ goto out_of_memory;14071407+ u->urb->transfer_buffer = ep->syncbuf + i * 4;14081408+ u->urb->transfer_dma = ep->sync_dma + i * 4;14091409+ u->urb->transfer_buffer_length = 4;14101410+ u->urb->pipe = ep->pipe;14111411+ u->urb->transfer_flags = URB_ISO_ASAP |14121412+ URB_NO_TRANSFER_DMA_MAP;14131413+ u->urb->number_of_packets = 1;14141414+ u->urb->interval = 1 << ep->syncinterval;14151415+ u->urb->context = u;14161416+ u->urb->complete = snd_complete_urb;14171417+ }14181418+14191419+ ep->nurbs = SYNC_URBS;14201420+14211421+ return 0;14221422+14231423+out_of_memory:14241424+ release_urbs(ep, 0);14251425+ return -ENOMEM;14261426+}14271427+14281428+/**14291429+ * snd_usb_endpoint_set_params: configure an snd_usb_endpoint14301430+ *14311431+ * @ep: the snd_usb_endpoint to configure14321432+ * @hw_params: the hardware parameters14331433+ * @fmt: the USB audio format information14341434+ * @sync_ep: the sync endpoint to use, if any14351435+ *14361436+ * Determine the number of URBs to be used on this endpoint.14371437+ * An endpoint must be configured before it can be started.14381438+ * An endpoint that is already running can not be reconfigured.14391439+ */14401440+int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,14411441+ struct snd_pcm_hw_params *hw_params,14421442+ struct audioformat *fmt,14431443+ struct snd_usb_endpoint *sync_ep)14441444+{14451445+ int err;14461446+14471447+ if (ep->use_count != 0) {14481448+ snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n",14491449+ ep->ep_num);14501450+ return -EBUSY;14511451+ }14521452+14531453+ /* release old buffers, if any */14541454+ release_urbs(ep, 0);14551455+14561456+ ep->datainterval = fmt->datainterval;14571457+ ep->maxpacksize = fmt->maxpacksize;14581458+ ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);14591459+14601460+ if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL)14611461+ ep->freqn = get_usb_full_speed_rate(params_rate(hw_params));14621462+ else14631463+ ep->freqn = get_usb_high_speed_rate(params_rate(hw_params));14641464+14651465+ /* calculate the frequency in 16.16 format */14661466+ ep->freqm = ep->freqn;14671467+ ep->freqshift = INT_MIN;14681468+14691469+ ep->phase = 0;14701470+14711471+ switch (ep->type) {14721472+ case SND_USB_ENDPOINT_TYPE_DATA:14731473+ err = data_ep_set_params(ep, hw_params, fmt, sync_ep);14741474+ break;14751475+ case SND_USB_ENDPOINT_TYPE_SYNC:14761476+ err = sync_ep_set_params(ep, hw_params, fmt);14771477+ break;14781478+ default:14791479+ err = -EINVAL;14801480+ }14811481+14821482+ snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n",14831483+ ep->ep_num, ep->type, ep->nurbs, err);14841484+14851485+ return err;14861486+}14871487+14881488+/**14891489+ * snd_usb_endpoint_start: start an snd_usb_endpoint14901490+ *14911491+ * @ep: the endpoint to start14921492+ *14931493+ * A call to this function will increment the use count of the endpoint.14941494+ * In case it is not already running, the URBs for this endpoint will be14951495+ * submitted. Otherwise, this function does nothing.14961496+ *14971497+ * Must be balanced to calls of snd_usb_endpoint_stop().14981498+ *14991499+ * Returns an error if the URB submission failed, 0 in all other cases.15001500+ */15011501+int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)15021502+{15031503+ int err;15041504+ unsigned int i;15051505+15061506+ if (ep->chip->shutdown)15071507+ return -EBADFD;15081508+15091509+ /* already running? */15101510+ if (++ep->use_count != 1)15111511+ return 0;15121512+15131513+ if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags)))15141514+ return -EINVAL;15151515+15161516+ /* just to be sure */15171517+ deactivate_urbs(ep, 0, 1);15181518+ wait_clear_urbs(ep);15191519+15201520+ ep->active_mask = 0;15211521+ ep->unlink_mask = 0;15221522+ ep->phase = 0;15231523+15241524+ /*15251525+ * If this endpoint has a data endpoint as implicit feedback source,15261526+ * don't start the urbs here. Instead, mark them all as available,15271527+ * wait for the record urbs to return and queue the playback urbs15281528+ * from that context.15291529+ */15301530+15311531+ set_bit(EP_FLAG_RUNNING, &ep->flags);15321532+15331533+ if (snd_usb_endpoint_implict_feedback_sink(ep)) {15341534+ for (i = 0; i < ep->nurbs; i++) {15351535+ struct snd_urb_ctx *ctx = ep->urb + i;15361536+ list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);15371537+ }15381538+15391539+ return 0;15401540+ }15411541+15421542+ for (i = 0; i < ep->nurbs; i++) {15431543+ struct urb *urb = ep->urb[i].urb;15441544+15451545+ if (snd_BUG_ON(!urb))15461546+ goto __error;15471547+15481548+ if (usb_pipeout(ep->pipe)) {15491549+ prepare_outbound_urb_sizes(ep, urb->context);15501550+ prepare_outbound_urb(ep, urb->context);15511551+ } else {15521552+ prepare_inbound_urb(ep, urb->context);15531553+ }15541554+15551555+ err = usb_submit_urb(urb, GFP_ATOMIC);1561556 if (err < 0) {157157- snd_printk(KERN_ERR "cannot submit datapipe "158158- "for urb %d, error %d: %s\n",15571557+ snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n",1591558 i, err, usb_error_string(err));1601559 goto __error;1611560 }162162- set_bit(i, &subs->active_mask);15611561+ set_bit(i, &ep->active_mask);1631562 }164164- if (subs->syncpipe) {165165- for (i = 0; i < SYNC_URBS; i++) {166166- err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);167167- if (err < 0) {168168- snd_printk(KERN_ERR "cannot submit syncpipe "169169- "for urb %d, error %d: %s\n",170170- i, err, usb_error_string(err));171171- goto __error;172172- }173173- set_bit(i + 16, &subs->active_mask);174174- }175175- }15631563+1761564 return 0;1771565178178- __error:179179- // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);180180- deactivate_urbs(subs, 0, 0);15661566+__error:15671567+ clear_bit(EP_FLAG_RUNNING, &ep->flags);15681568+ ep->use_count--;15691569+ deactivate_urbs(ep, 0, 0);1811570 return -EPIPE;1821571}1831572184184-185185-/*15731573+/**15741574+ * snd_usb_endpoint_stop: stop an snd_usb_endpoint15751575+ *15761576+ * @ep: the endpoint to stop (may be NULL)15771577+ *15781578+ * A call to this function will decrement the use count of the endpoint.15791579+ * In case the last user has requested the endpoint stop, the URBs will15801580+ * actually be deactivated.15811581+ *15821582+ * Must be balanced to calls of snd_usb_endpoint_start().1861583 */187187-static struct snd_urb_ops audio_urb_ops[2] = {188188- {189189- .prepare = prepare_nodata_playback_urb,190190- .retire = retire_playback_urb,191191- .prepare_sync = prepare_playback_sync_urb,192192- .retire_sync = retire_playback_sync_urb,193193- },194194- {195195- .prepare = prepare_capture_urb,196196- .retire = retire_capture_urb,197197- .prepare_sync = prepare_capture_sync_urb,198198- .retire_sync = retire_capture_sync_urb,199199- },200200-};15841584+void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep,15851585+ int force, int can_sleep, int wait)15861586+{15871587+ if (!ep)15881588+ return;2011589202202-/*203203- * initialize the substream instance.15901590+ if (snd_BUG_ON(ep->use_count == 0))15911591+ return;15921592+15931593+ if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags)))15941594+ return;15951595+15961596+ if (--ep->use_count == 0) {15971597+ deactivate_urbs(ep, force, can_sleep);15981598+ ep->data_subs = NULL;15991599+ ep->sync_slave = NULL;16001600+ ep->retire_data_urb = NULL;16011601+ ep->prepare_data_urb = NULL;16021602+16031603+ if (wait)16041604+ wait_clear_urbs(ep);16051605+ }16061606+}16071607+16081608+/**16091609+ * snd_usb_endpoint_activate: activate an snd_usb_endpoint16101610+ *16111611+ * @ep: the endpoint to activate16121612+ *16131613+ * If the endpoint is not currently in use, this functions will select the16141614+ * correct alternate interface setting for the interface of this endpoint.16151615+ *16161616+ * In case of any active users, this functions does nothing.16171617+ *16181618+ * Returns an error if usb_set_interface() failed, 0 in all other16191619+ * cases.2041620 */205205-206206-void snd_usb_init_substream(struct snd_usb_stream *as,207207- int stream, struct audioformat *fp)16211621+int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep)2081622{209209- struct snd_usb_substream *subs = &as->substream[stream];210210-211211- INIT_LIST_HEAD(&subs->fmt_list);212212- spin_lock_init(&subs->lock);213213-214214- subs->stream = as;215215- subs->direction = stream;216216- subs->dev = as->chip->dev;217217- subs->txfr_quirk = as->chip->txfr_quirk;218218- subs->ops = audio_urb_ops[stream];219219- if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH)220220- subs->ops.prepare_sync = prepare_capture_sync_urb_hs;221221-222222- snd_usb_set_pcm_ops(as->pcm, stream);223223-224224- list_add_tail(&fp->list, &subs->fmt_list);225225- subs->formats |= fp->formats;226226- subs->endpoint = fp->endpoint;227227- subs->num_formats++;228228- subs->fmt_type = fp->fmt_type;229229-}230230-231231-int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)232232-{233233- struct snd_usb_substream *subs = substream->runtime->private_data;234234-235235- switch (cmd) {236236- case SNDRV_PCM_TRIGGER_START:237237- case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:238238- subs->ops.prepare = prepare_playback_urb;16231623+ if (ep->use_count != 0)2391624 return 0;240240- case SNDRV_PCM_TRIGGER_STOP:241241- return deactivate_urbs(subs, 0, 0);242242- case SNDRV_PCM_TRIGGER_PAUSE_PUSH:243243- subs->ops.prepare = prepare_nodata_playback_urb;16251625+16261626+ if (!ep->chip->shutdown &&16271627+ !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) {16281628+ int ret;16291629+16301630+ ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx);16311631+ if (ret < 0) {16321632+ snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n",16331633+ __func__, ret);16341634+ clear_bit(EP_FLAG_ACTIVATED, &ep->flags);16351635+ return ret;16361636+ }16371637+2441638 return 0;2451639 }2461640247247- return -EINVAL;16411641+ return -EBUSY;2481642}2491643250250-int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)16441644+/**16451645+ * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint16461646+ *16471647+ * @ep: the endpoint to deactivate16481648+ *16491649+ * If the endpoint is not currently in use, this functions will select the16501650+ * alternate interface setting 0 for the interface of this endpoint.16511651+ *16521652+ * In case of any active users, this functions does nothing.16531653+ *16541654+ * Returns an error if usb_set_interface() failed, 0 in all other16551655+ * cases.16561656+ */16571657+int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)2511658{252252- struct snd_usb_substream *subs = substream->runtime->private_data;16591659+ if (!ep)16601660+ return -EINVAL;2531661254254- switch (cmd) {255255- case SNDRV_PCM_TRIGGER_START:256256- subs->ops.retire = retire_capture_urb;257257- return start_urbs(subs, substream->runtime);258258- case SNDRV_PCM_TRIGGER_STOP:259259- return deactivate_urbs(subs, 0, 0);260260- case SNDRV_PCM_TRIGGER_PAUSE_PUSH:261261- subs->ops.retire = retire_paused_capture_urb;16621662+ if (ep->use_count != 0)2621663 return 0;263263- case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:264264- subs->ops.retire = retire_capture_urb;16641664+16651665+ if (!ep->chip->shutdown &&16661666+ test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) {16671667+ int ret;16681668+16691669+ ret = usb_set_interface(ep->chip->dev, ep->iface, 0);16701670+ if (ret < 0) {16711671+ snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n",16721672+ __func__, ret);16731673+ return ret;16741674+ }16751675+2651676 return 0;2661677 }2671678268268- return -EINVAL;16791679+ return -EBUSY;2691680}2701681271271-int snd_usb_substream_prepare(struct snd_usb_substream *subs,272272- struct snd_pcm_runtime *runtime)16821682+/**16831683+ * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint16841684+ *16851685+ * @ep: the list header of the endpoint to free16861686+ *16871687+ * This function does not care for the endpoint's use count but will tear16881688+ * down all the streaming URBs immediately and free all resources.16891689+ */16901690+void snd_usb_endpoint_free(struct list_head *head)2731691{274274- /* clear urbs (to be sure) */275275- deactivate_urbs(subs, 0, 1);276276- wait_clear_urbs(subs);16921692+ struct snd_usb_endpoint *ep;2771693278278- /* for playback, submit the URBs now; otherwise, the first hwptr_done279279- * updates for all URBs would happen at the same time when starting */280280- if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {281281- subs->ops.prepare = prepare_nodata_playback_urb;282282- return start_urbs(subs, runtime);16941694+ ep = list_entry(head, struct snd_usb_endpoint, list);16951695+ release_urbs(ep, 1);16961696+ kfree(ep);16971697+}16981698+16991699+/**17001700+ * snd_usb_handle_sync_urb: parse an USB sync packet17011701+ *17021702+ * @ep: the endpoint to handle the packet17031703+ * @sender: the sending endpoint17041704+ * @urb: the received packet17051705+ *17061706+ * This function is called from the context of an endpoint that received17071707+ * the packet and is used to let another endpoint object handle the payload.17081708+ */17091709+void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,17101710+ struct snd_usb_endpoint *sender,17111711+ const struct urb *urb)17121712+{17131713+ int shift;17141714+ unsigned int f;17151715+ unsigned long flags;17161716+17171717+ snd_BUG_ON(ep == sender);17181718+17191719+ /*17201720+ * In case the endpoint is operating in implicit feedback mode, prepare17211721+ * a new outbound URB that has the same layout as the received packet17221722+ * and add it to the list of pending urbs. queue_pending_output_urbs()17231723+ * will take care of them later.17241724+ */17251725+ if (snd_usb_endpoint_implict_feedback_sink(ep) &&17261726+ ep->use_count != 0) {17271727+17281728+ /* implicit feedback case */17291729+ int i, bytes = 0;17301730+ struct snd_urb_ctx *in_ctx;17311731+ struct snd_usb_packet_info *out_packet;17321732+17331733+ in_ctx = urb->context;17341734+17351735+ /* Count overall packet size */17361736+ for (i = 0; i < in_ctx->packets; i++)17371737+ if (urb->iso_frame_desc[i].status == 0)17381738+ bytes += urb->iso_frame_desc[i].actual_length;17391739+17401740+ /*17411741+ * skip empty packets. At least M-Audio's Fast Track Ultra stops17421742+ * streaming once it received a 0-byte OUT URB17431743+ */17441744+ if (bytes == 0)17451745+ return;17461746+17471747+ spin_lock_irqsave(&ep->lock, flags);17481748+ out_packet = ep->next_packet + ep->next_packet_write_pos;17491749+17501750+ /*17511751+ * Iterate through the inbound packet and prepare the lengths17521752+ * for the output packet. The OUT packet we are about to send17531753+ * will have the same amount of payload bytes than the IN17541754+ * packet we just received.17551755+ */17561756+17571757+ out_packet->packets = in_ctx->packets;17581758+ for (i = 0; i < in_ctx->packets; i++) {17591759+ if (urb->iso_frame_desc[i].status == 0)17601760+ out_packet->packet_size[i] =17611761+ urb->iso_frame_desc[i].actual_length / ep->stride;17621762+ else17631763+ out_packet->packet_size[i] = 0;17641764+ }17651765+17661766+ ep->next_packet_write_pos++;17671767+ ep->next_packet_write_pos %= MAX_URBS;17681768+ spin_unlock_irqrestore(&ep->lock, flags);17691769+ queue_pending_output_urbs(ep);17701770+17711771+ return;2831772 }2841773285285- return 0;17741774+ /*17751775+ * process after playback sync complete17761776+ *17771777+ * Full speed devices report feedback values in 10.14 format as samples17781778+ * per frame, high speed devices in 16.16 format as samples per17791779+ * microframe.17801780+ *17811781+ * Because the Audio Class 1 spec was written before USB 2.0, many high17821782+ * speed devices use a wrong interpretation, some others use an17831783+ * entirely different format.17841784+ *17851785+ * Therefore, we cannot predict what format any particular device uses17861786+ * and must detect it automatically.17871787+ */17881788+17891789+ if (urb->iso_frame_desc[0].status != 0 ||17901790+ urb->iso_frame_desc[0].actual_length < 3)17911791+ return;17921792+17931793+ f = le32_to_cpup(urb->transfer_buffer);17941794+ if (urb->iso_frame_desc[0].actual_length == 3)17951795+ f &= 0x00ffffff;17961796+ else17971797+ f &= 0x0fffffff;17981798+17991799+ if (f == 0)18001800+ return;18011801+18021802+ if (unlikely(ep->freqshift == INT_MIN)) {18031803+ /*18041804+ * The first time we see a feedback value, determine its format18051805+ * by shifting it left or right until it matches the nominal18061806+ * frequency value. This assumes that the feedback does not18071807+ * differ from the nominal value more than +50% or -25%.18081808+ */18091809+ shift = 0;18101810+ while (f < ep->freqn - ep->freqn / 4) {18111811+ f <<= 1;18121812+ shift++;18131813+ }18141814+ while (f > ep->freqn + ep->freqn / 2) {18151815+ f >>= 1;18161816+ shift--;18171817+ }18181818+ ep->freqshift = shift;18191819+ } else if (ep->freqshift >= 0)18201820+ f <<= ep->freqshift;18211821+ else18221822+ f >>= -ep->freqshift;18231823+18241824+ if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {18251825+ /*18261826+ * If the frequency looks valid, set it.18271827+ * This value is referred to in prepare_playback_urb().18281828+ */18291829+ spin_lock_irqsave(&ep->lock, flags);18301830+ ep->freqm = f;18311831+ spin_unlock_irqrestore(&ep->lock, flags);18321832+ } else {18331833+ /*18341834+ * Out of range; maybe the shift value is wrong.18351835+ * Reset it so that we autodetect again the next time.18361836+ */18371837+ ep->freqshift = INT_MIN;18381838+ }2861839}2871840
+20-12
sound/usb/endpoint.h
···11#ifndef __USBAUDIO_ENDPOINT_H22#define __USBAUDIO_ENDPOINT_H3344-void snd_usb_init_substream(struct snd_usb_stream *as,55- int stream,66- struct audioformat *fp);44+#define SND_USB_ENDPOINT_TYPE_DATA 055+#define SND_USB_ENDPOINT_TYPE_SYNC 17688-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);77+struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,88+ struct usb_host_interface *alts,99+ int ep_num, int direction, int type);12101313-void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force);1111+int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,1212+ struct snd_pcm_hw_params *hw_params,1313+ struct audioformat *fmt,1414+ struct snd_usb_endpoint *sync_ep);14151515-int snd_usb_substream_prepare(struct snd_usb_substream *subs,1616- struct snd_pcm_runtime *runtime);1616+int snd_usb_endpoint_start(struct snd_usb_endpoint *ep);1717+void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep,1818+ int force, int can_sleep, int wait);1919+int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep);2020+int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep);2121+void snd_usb_endpoint_free(struct list_head *head);17221818-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);2323+int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep);2424+2525+void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,2626+ struct snd_usb_endpoint *sender,2727+ const struct urb *urb);20282129#endif /* __USBAUDIO_ENDPOINT_H */
+35-15
sound/usb/mixer.c
···486486/*487487 * TLV callback for mixer volume controls488488 */489489-static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,489489+int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,490490 unsigned int size, unsigned int __user *_tlv)491491{492492 struct usb_mixer_elem_info *cval = kcontrol->private_data;···770770 struct snd_kcontrol *kctl)771771{772772 switch (cval->mixer->chip->usb_id) {773773+ case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */774774+ case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */775775+ if (strcmp(kctl->id.name, "Effect Duration") == 0) {776776+ snd_printk(KERN_INFO777777+ "usb-audio: set quirk for FTU Effect Duration\n");778778+ cval->min = 0x0000;779779+ cval->max = 0x7f00;780780+ cval->res = 0x0100;781781+ break;782782+ }783783+ if (strcmp(kctl->id.name, "Effect Volume") == 0 ||784784+ strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {785785+ snd_printk(KERN_INFO786786+ "usb-audio: set quirks for FTU Effect Feedback/Volume\n");787787+ cval->min = 0x00;788788+ cval->max = 0x7f;789789+ break;790790+ }791791+ break;792792+773793 case USB_ID(0x0471, 0x0101):774794 case USB_ID(0x0471, 0x0104):775795 case USB_ID(0x0471, 0x0105):···11411121 len = snd_usb_copy_string_desc(state, nameid,11421122 kctl->id.name, sizeof(kctl->id.name));1143112311441144- /* get min/max values */11451145- get_min_max_with_quirks(cval, 0, kctl);11461146-11471124 switch (control) {11481125 case UAC_FU_MUTE:11491126 case UAC_FU_VOLUME:···11721155 }11731156 append_ctl_name(kctl, control == UAC_FU_MUTE ?11741157 " Switch" : " Volume");11751175- if (control == UAC_FU_VOLUME) {11761176- check_mapped_dB(map, cval);11771177- if (cval->dBmin < cval->dBmax || !cval->initialized) {11781178- kctl->tlv.c = mixer_vol_tlv;11791179- kctl->vd[0].access |= 11801180- SNDRV_CTL_ELEM_ACCESS_TLV_READ |11811181- SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;11821182- }11831183- }11841158 break;11851185-11861159 default:11871160 if (! len)11881161 strlcpy(kctl->id.name, audio_feature_info[control-1].name,11891162 sizeof(kctl->id.name));11901163 break;11641164+ }11651165+11661166+ /* get min/max values */11671167+ get_min_max_with_quirks(cval, 0, kctl);11681168+11691169+ if (control == UAC_FU_VOLUME) {11701170+ check_mapped_dB(map, cval);11711171+ if (cval->dBmin < cval->dBmax || !cval->initialized) {11721172+ kctl->tlv.c = snd_usb_mixer_vol_tlv;11731173+ kctl->vd[0].access |=11741174+ SNDRV_CTL_ELEM_ACCESS_TLV_READ |11751175+ SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;11761176+ }11911177 }1192117811931179 range = (cval->max - cval->min) / cval->res;···14081388 for (pin = 0; pin < input_pins; pin++) {14091389 err = parse_audio_unit(state, desc->baSourceID[pin]);14101390 if (err < 0)14111411- return err;13911391+ continue;14121392 err = check_input_term(state, desc->baSourceID[pin], &iterm);14131393 if (err < 0)14141394 return err;
+3
sound/usb/mixer.h
···6868int snd_usb_mixer_add_control(struct usb_mixer_interface *mixer,6969 struct snd_kcontrol *kctl);70707171+int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,7272+ unsigned int size, unsigned int __user *_tlv);7373+7174#endif /* __USBMIXER_H */
+13
sound/usb/mixer_maps.c
···288288 { 0 } /* terminator */289289};290290291291+static struct usbmix_name_map ebox44_map[] = {292292+ { 4, NULL }, /* FU */293293+ { 6, NULL }, /* MU */294294+ { 7, NULL }, /* FU */295295+ { 10, NULL }, /* FU */296296+ { 11, NULL }, /* MU */297297+ { 0 }298298+};299299+291300/* "Gamesurround Muse Pocket LT" looks same like "Sound Blaster MP3+"292301 * most importand difference is SU[8], it should be set to "Capture Source"293302 * to make alsamixer and PA working properly.···379370 .id = USB_ID(0x13e5, 0x0001),380371 .map = scratch_live_map,381372 .ignore_ctl_error = 1,373373+ },374374+ {375375+ .id = USB_ID(0x200c, 0x1018),376376+ .map = ebox44_map,382377 },383378 { 0 } /* terminator */384379};
+446-28
sound/usb/mixer_quirks.c
···42424343extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;44444545+/* private_free callback */4646+static void usb_mixer_elem_free(struct snd_kcontrol *kctl)4747+{4848+ kfree(kctl->private_data);4949+ kctl->private_data = NULL;5050+}5151+5252+/* This function allows for the creation of standard UAC controls.5353+ * See the quirks for M-Audio FTUs or Ebox-44.5454+ * If you don't want to set a TLV callback pass NULL.5555+ *5656+ * Since there doesn't seem to be a devices that needs a multichannel5757+ * version, we keep it mono for simplicity.5858+ */5959+static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,6060+ unsigned int unitid,6161+ unsigned int control,6262+ unsigned int cmask,6363+ int val_type,6464+ const char *name,6565+ snd_kcontrol_tlv_rw_t *tlv_callback)6666+{6767+ int err;6868+ struct usb_mixer_elem_info *cval;6969+ struct snd_kcontrol *kctl;7070+7171+ cval = kzalloc(sizeof(*cval), GFP_KERNEL);7272+ if (!cval)7373+ return -ENOMEM;7474+7575+ cval->id = unitid;7676+ cval->mixer = mixer;7777+ cval->val_type = val_type;7878+ cval->channels = 1;7979+ cval->control = control;8080+ cval->cmask = cmask;8181+8282+ /* get_min_max() is called only for integer volumes later,8383+ * so provide a short-cut for booleans */8484+ cval->min = 0;8585+ cval->max = 1;8686+ cval->res = 0;8787+ cval->dBmin = 0;8888+ cval->dBmax = 0;8989+9090+ /* Create control */9191+ kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);9292+ if (!kctl) {9393+ kfree(cval);9494+ return -ENOMEM;9595+ }9696+9797+ /* Set name */9898+ snprintf(kctl->id.name, sizeof(kctl->id.name), name);9999+ kctl->private_free = usb_mixer_elem_free;100100+101101+ /* set TLV */102102+ if (tlv_callback) {103103+ kctl->tlv.c = tlv_callback;104104+ kctl->vd[0].access |=105105+ SNDRV_CTL_ELEM_ACCESS_TLV_READ |106106+ SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;107107+ }108108+ /* Add control to mixer */109109+ err = snd_usb_mixer_add_control(mixer, kctl);110110+ if (err < 0)111111+ return err;112112+113113+ return 0;114114+}115115+45116/*46117 * Sound Blaster remote control configuration47118 *···566495}567496568497/* M-Audio FastTrack Ultra quirks */498498+/* FTU Effect switch */499499+struct snd_ftu_eff_switch_priv_val {500500+ struct usb_mixer_interface *mixer;501501+ int cached_value;502502+ int is_cached;503503+};569504570570-/* private_free callback */571571-static void usb_mixer_elem_free(struct snd_kcontrol *kctl)505505+static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,506506+ struct snd_ctl_elem_info *uinfo)572507{573573- kfree(kctl->private_data);574574- kctl->private_data = NULL;508508+ static const char *texts[8] = {"Room 1",509509+ "Room 2",510510+ "Room 3",511511+ "Hall 1",512512+ "Hall 2",513513+ "Plate",514514+ "Delay",515515+ "Echo"516516+ };517517+518518+ uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;519519+ uinfo->count = 1;520520+ uinfo->value.enumerated.items = 8;521521+ if (uinfo->value.enumerated.item > 7)522522+ uinfo->value.enumerated.item = 7;523523+ strcpy(uinfo->value.enumerated.name,524524+ texts[uinfo->value.enumerated.item]);525525+526526+ return 0;575527}576528577577-static int snd_maudio_ftu_create_ctl(struct usb_mixer_interface *mixer,578578- int in, int out, const char *name)529529+static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,530530+ struct snd_ctl_elem_value *ucontrol)579531{580580- struct usb_mixer_elem_info *cval;581581- struct snd_kcontrol *kctl;532532+ struct snd_usb_audio *chip;533533+ struct usb_mixer_interface *mixer;534534+ struct snd_ftu_eff_switch_priv_val *pval;535535+ int err;536536+ unsigned char value[2];582537583583- cval = kzalloc(sizeof(*cval), GFP_KERNEL);584584- if (!cval)538538+ const int id = 6;539539+ const int validx = 1;540540+ const int val_len = 2;541541+542542+ value[0] = 0x00;543543+ value[1] = 0x00;544544+545545+ pval = (struct snd_ftu_eff_switch_priv_val *)546546+ kctl->private_value;547547+548548+ if (pval->is_cached) {549549+ ucontrol->value.enumerated.item[0] = pval->cached_value;550550+ return 0;551551+ }552552+553553+ mixer = (struct usb_mixer_interface *) pval->mixer;554554+ if (snd_BUG_ON(!mixer))555555+ return -EINVAL;556556+557557+ chip = (struct snd_usb_audio *) mixer->chip;558558+ if (snd_BUG_ON(!chip))559559+ return -EINVAL;560560+561561+562562+ err = snd_usb_ctl_msg(chip->dev,563563+ usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,564564+ USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,565565+ validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),566566+ value, val_len);567567+ if (err < 0)568568+ return err;569569+570570+ ucontrol->value.enumerated.item[0] = value[0];571571+ pval->cached_value = value[0];572572+ pval->is_cached = 1;573573+574574+ return 0;575575+}576576+577577+static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,578578+ struct snd_ctl_elem_value *ucontrol)579579+{580580+ struct snd_usb_audio *chip;581581+ struct snd_ftu_eff_switch_priv_val *pval;582582+583583+ struct usb_mixer_interface *mixer;584584+ int changed, cur_val, err, new_val;585585+ unsigned char value[2];586586+587587+588588+ const int id = 6;589589+ const int validx = 1;590590+ const int val_len = 2;591591+592592+ changed = 0;593593+594594+ pval = (struct snd_ftu_eff_switch_priv_val *)595595+ kctl->private_value;596596+ cur_val = pval->cached_value;597597+ new_val = ucontrol->value.enumerated.item[0];598598+599599+ mixer = (struct usb_mixer_interface *) pval->mixer;600600+ if (snd_BUG_ON(!mixer))601601+ return -EINVAL;602602+603603+ chip = (struct snd_usb_audio *) mixer->chip;604604+ if (snd_BUG_ON(!chip))605605+ return -EINVAL;606606+607607+ if (!pval->is_cached) {608608+ /* Read current value */609609+ err = snd_usb_ctl_msg(chip->dev,610610+ usb_rcvctrlpipe(chip->dev, 0), UAC_GET_CUR,611611+ USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,612612+ validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),613613+ value, val_len);614614+ if (err < 0)615615+ return err;616616+617617+ cur_val = value[0];618618+ pval->cached_value = cur_val;619619+ pval->is_cached = 1;620620+ }621621+ /* update value if needed */622622+ if (cur_val != new_val) {623623+ value[0] = new_val;624624+ value[1] = 0;625625+ err = snd_usb_ctl_msg(chip->dev,626626+ usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,627627+ USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,628628+ validx << 8, snd_usb_ctrl_intf(chip) | (id << 8),629629+ value, val_len);630630+ if (err < 0)631631+ return err;632632+633633+ pval->cached_value = new_val;634634+ pval->is_cached = 1;635635+ changed = 1;636636+ }637637+638638+ return changed;639639+}640640+641641+static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer)642642+{643643+ static struct snd_kcontrol_new template = {644644+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,645645+ .name = "Effect Program Switch",646646+ .index = 0,647647+ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,648648+ .info = snd_ftu_eff_switch_info,649649+ .get = snd_ftu_eff_switch_get,650650+ .put = snd_ftu_eff_switch_put651651+ };652652+653653+ int err;654654+ struct snd_kcontrol *kctl;655655+ struct snd_ftu_eff_switch_priv_val *pval;656656+657657+ pval = kzalloc(sizeof(*pval), GFP_KERNEL);658658+ if (!pval)585659 return -ENOMEM;586660587587- cval->id = 5;588588- cval->mixer = mixer;589589- cval->val_type = USB_MIXER_S16;590590- cval->channels = 1;591591- cval->control = out + 1;592592- cval->cmask = 1 << in;661661+ pval->cached_value = 0;662662+ pval->is_cached = 0;663663+ pval->mixer = mixer;593664594594- kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);665665+ template.private_value = (unsigned long) pval;666666+ kctl = snd_ctl_new1(&template, mixer->chip);595667 if (!kctl) {596596- kfree(cval);668668+ kfree(pval);597669 return -ENOMEM;598670 }599671600600- snprintf(kctl->id.name, sizeof(kctl->id.name), name);601601- kctl->private_free = usb_mixer_elem_free;602602- return snd_usb_mixer_add_control(mixer, kctl);672672+ err = snd_ctl_add(mixer->chip->card, kctl);673673+ if (err < 0)674674+ return err;675675+676676+ return 0;603677}604678605605-static int snd_maudio_ftu_create_mixer(struct usb_mixer_interface *mixer)679679+/* Create volume controls for FTU devices*/680680+static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)606681{607682 char name[64];683683+ unsigned int control, cmask;608684 int in, out, err;609685686686+ const unsigned int id = 5;687687+ const int val_type = USB_MIXER_S16;688688+610689 for (out = 0; out < 8; out++) {690690+ control = out + 1;611691 for (in = 0; in < 8; in++) {692692+ cmask = 1 << in;612693 snprintf(name, sizeof(name),613613- "AIn%d - Out%d Capture Volume", in + 1, out + 1);614614- err = snd_maudio_ftu_create_ctl(mixer, in, out, name);694694+ "AIn%d - Out%d Capture Volume",695695+ in + 1, out + 1);696696+ err = snd_create_std_mono_ctl(mixer, id, control,697697+ cmask, val_type, name,698698+ &snd_usb_mixer_vol_tlv);615699 if (err < 0)616700 return err;617701 }618618-619702 for (in = 8; in < 16; in++) {703703+ cmask = 1 << in;620704 snprintf(name, sizeof(name),621621- "DIn%d - Out%d Playback Volume", in - 7, out + 1);622622- err = snd_maudio_ftu_create_ctl(mixer, in, out, name);705705+ "DIn%d - Out%d Playback Volume",706706+ in - 7, out + 1);707707+ err = snd_create_std_mono_ctl(mixer, id, control,708708+ cmask, val_type, name,709709+ &snd_usb_mixer_vol_tlv);623710 if (err < 0)624711 return err;625712 }626713 }714714+715715+ return 0;716716+}717717+718718+/* This control needs a volume quirk, see mixer.c */719719+static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)720720+{721721+ static const char name[] = "Effect Volume";722722+ const unsigned int id = 6;723723+ const int val_type = USB_MIXER_U8;724724+ const unsigned int control = 2;725725+ const unsigned int cmask = 0;726726+727727+ return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,728728+ name, snd_usb_mixer_vol_tlv);729729+}730730+731731+/* This control needs a volume quirk, see mixer.c */732732+static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)733733+{734734+ static const char name[] = "Effect Duration";735735+ const unsigned int id = 6;736736+ const int val_type = USB_MIXER_S16;737737+ const unsigned int control = 3;738738+ const unsigned int cmask = 0;739739+740740+ return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,741741+ name, snd_usb_mixer_vol_tlv);742742+}743743+744744+/* This control needs a volume quirk, see mixer.c */745745+static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)746746+{747747+ static const char name[] = "Effect Feedback Volume";748748+ const unsigned int id = 6;749749+ const int val_type = USB_MIXER_U8;750750+ const unsigned int control = 4;751751+ const unsigned int cmask = 0;752752+753753+ return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,754754+ name, NULL);755755+}756756+757757+static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)758758+{759759+ unsigned int cmask;760760+ int err, ch;761761+ char name[48];762762+763763+ const unsigned int id = 7;764764+ const int val_type = USB_MIXER_S16;765765+ const unsigned int control = 7;766766+767767+ for (ch = 0; ch < 4; ++ch) {768768+ cmask = 1 << ch;769769+ snprintf(name, sizeof(name),770770+ "Effect Return %d Volume", ch + 1);771771+ err = snd_create_std_mono_ctl(mixer, id, control,772772+ cmask, val_type, name,773773+ snd_usb_mixer_vol_tlv);774774+ if (err < 0)775775+ return err;776776+ }777777+778778+ return 0;779779+}780780+781781+static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)782782+{783783+ unsigned int cmask;784784+ int err, ch;785785+ char name[48];786786+787787+ const unsigned int id = 5;788788+ const int val_type = USB_MIXER_S16;789789+ const unsigned int control = 9;790790+791791+ for (ch = 0; ch < 8; ++ch) {792792+ cmask = 1 << ch;793793+ snprintf(name, sizeof(name),794794+ "Effect Send AIn%d Volume", ch + 1);795795+ err = snd_create_std_mono_ctl(mixer, id, control, cmask,796796+ val_type, name,797797+ snd_usb_mixer_vol_tlv);798798+ if (err < 0)799799+ return err;800800+ }801801+ for (ch = 8; ch < 16; ++ch) {802802+ cmask = 1 << ch;803803+ snprintf(name, sizeof(name),804804+ "Effect Send DIn%d Volume", ch - 7);805805+ err = snd_create_std_mono_ctl(mixer, id, control, cmask,806806+ val_type, name,807807+ snd_usb_mixer_vol_tlv);808808+ if (err < 0)809809+ return err;810810+ }811811+ return 0;812812+}813813+814814+static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)815815+{816816+ int err;817817+818818+ err = snd_ftu_create_volume_ctls(mixer);819819+ if (err < 0)820820+ return err;821821+822822+ err = snd_ftu_create_effect_switch(mixer);823823+ if (err < 0)824824+ return err;825825+ err = snd_ftu_create_effect_volume_ctl(mixer);826826+ if (err < 0)827827+ return err;828828+829829+ err = snd_ftu_create_effect_duration_ctl(mixer);830830+ if (err < 0)831831+ return err;832832+833833+ err = snd_ftu_create_effect_feedback_ctl(mixer);834834+ if (err < 0)835835+ return err;836836+837837+ err = snd_ftu_create_effect_return_ctls(mixer);838838+ if (err < 0)839839+ return err;840840+841841+ err = snd_ftu_create_effect_send_ctls(mixer);842842+ if (err < 0)843843+ return err;844844+845845+ return 0;846846+}847847+848848+849849+/*850850+ * Create mixer for Electrix Ebox-44851851+ *852852+ * The mixer units from this device are corrupt, and even where they853853+ * are valid they presents mono controls as L and R channels of854854+ * stereo. So we create a good mixer in code.855855+ */856856+857857+static int snd_ebox44_create_mixer(struct usb_mixer_interface *mixer)858858+{859859+ int err;860860+861861+ err = snd_create_std_mono_ctl(mixer, 4, 1, 0x0, USB_MIXER_INV_BOOLEAN,862862+ "Headphone Playback Switch", NULL);863863+ if (err < 0)864864+ return err;865865+ err = snd_create_std_mono_ctl(mixer, 4, 2, 0x1, USB_MIXER_S16,866866+ "Headphone A Mix Playback Volume", NULL);867867+ if (err < 0)868868+ return err;869869+ err = snd_create_std_mono_ctl(mixer, 4, 2, 0x2, USB_MIXER_S16,870870+ "Headphone B Mix Playback Volume", NULL);871871+ if (err < 0)872872+ return err;873873+874874+ err = snd_create_std_mono_ctl(mixer, 7, 1, 0x0, USB_MIXER_INV_BOOLEAN,875875+ "Output Playback Switch", NULL);876876+ if (err < 0)877877+ return err;878878+ err = snd_create_std_mono_ctl(mixer, 7, 2, 0x1, USB_MIXER_S16,879879+ "Output A Playback Volume", NULL);880880+ if (err < 0)881881+ return err;882882+ err = snd_create_std_mono_ctl(mixer, 7, 2, 0x2, USB_MIXER_S16,883883+ "Output B Playback Volume", NULL);884884+ if (err < 0)885885+ return err;886886+887887+ err = snd_create_std_mono_ctl(mixer, 10, 1, 0x0, USB_MIXER_INV_BOOLEAN,888888+ "Input Capture Switch", NULL);889889+ if (err < 0)890890+ return err;891891+ err = snd_create_std_mono_ctl(mixer, 10, 2, 0x1, USB_MIXER_S16,892892+ "Input A Capture Volume", NULL);893893+ if (err < 0)894894+ return err;895895+ err = snd_create_std_mono_ctl(mixer, 10, 2, 0x2, USB_MIXER_S16,896896+ "Input B Capture Volume", NULL);897897+ if (err < 0)898898+ return err;627899628900 return 0;629901}···10146001015601 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */1016602 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */10171017- err = snd_maudio_ftu_create_mixer(mixer);603603+ err = snd_ftu_create_mixer(mixer);1018604 break;10196051020606 case USB_ID(0x0b05, 0x1739):···1032618 err = snd_nativeinstruments_create_mixer(mixer,1033619 snd_nativeinstruments_ta10_mixers,1034620 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));621621+ break;622622+623623+ case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */624624+ err = snd_ebox44_create_mixer(mixer);1035625 break;1036626 }1037627
+376-75
sound/usb/pcm.c
···16161717#include <linux/init.h>1818#include <linux/slab.h>1919+#include <linux/ratelimit.h>1920#include <linux/usb.h>2021#include <linux/usb/audio.h>2122#include <linux/usb/audio-v2.h>···3433#include "pcm.h"3534#include "clock.h"3635#include "power.h"3636+3737+#define SUBSTREAM_FLAG_DATA_EP_STARTED 03838+#define SUBSTREAM_FLAG_SYNC_EP_STARTED 137393840/* return the estimated delay based on USB frame counters */3941snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,···212208 }213209}214210211211+static int start_endpoints(struct snd_usb_substream *subs)212212+{213213+ int err;214214+215215+ if (!subs->data_endpoint)216216+ return -EINVAL;217217+218218+ if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {219219+ struct snd_usb_endpoint *ep = subs->data_endpoint;220220+221221+ snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);222222+223223+ ep->data_subs = subs;224224+ err = snd_usb_endpoint_start(ep);225225+ if (err < 0) {226226+ clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);227227+ return err;228228+ }229229+ }230230+231231+ if (subs->sync_endpoint &&232232+ !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {233233+ struct snd_usb_endpoint *ep = subs->sync_endpoint;234234+235235+ snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);236236+237237+ ep->sync_slave = subs->data_endpoint;238238+ err = snd_usb_endpoint_start(ep);239239+ if (err < 0) {240240+ clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);241241+ return err;242242+ }243243+ }244244+245245+ return 0;246246+}247247+248248+static void stop_endpoints(struct snd_usb_substream *subs,249249+ int force, int can_sleep, int wait)250250+{251251+ if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))252252+ snd_usb_endpoint_stop(subs->sync_endpoint,253253+ force, can_sleep, wait);254254+255255+ if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))256256+ snd_usb_endpoint_stop(subs->data_endpoint,257257+ force, can_sleep, wait);258258+}259259+260260+static int activate_endpoints(struct snd_usb_substream *subs)261261+{262262+ if (subs->sync_endpoint) {263263+ int ret;264264+265265+ ret = snd_usb_endpoint_activate(subs->sync_endpoint);266266+ if (ret < 0)267267+ return ret;268268+ }269269+270270+ return snd_usb_endpoint_activate(subs->data_endpoint);271271+}272272+273273+static int deactivate_endpoints(struct snd_usb_substream *subs)274274+{275275+ int reta, retb;276276+277277+ reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);278278+ retb = snd_usb_endpoint_deactivate(subs->data_endpoint);279279+280280+ if (reta < 0)281281+ return reta;282282+283283+ if (retb < 0)284284+ return retb;285285+286286+ return 0;287287+}288288+215289/*216290 * find a matching format and set up the interface217291 */···301219 struct usb_interface *iface;302220 unsigned int ep, attr;303221 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;304304- int err;222222+ int err, implicit_fb = 0;305223306224 iface = usb_ifnum_to_if(dev, fmt->iface);307225 if (WARN_ON(!iface))···314232 if (fmt == subs->cur_audiofmt)315233 return 0;316234317317- /* close the old interface */318318- if (subs->interface >= 0 && subs->interface != fmt->iface) {319319- if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {320320- snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",321321- dev->devnum, fmt->iface, fmt->altsetting);322322- return -EIO;323323- }324324- subs->interface = -1;325325- subs->altset_idx = 0;326326- }327327-328328- /* set interface */329329- if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {330330- if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {331331- snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",332332- dev->devnum, fmt->iface, fmt->altsetting);333333- return -EIO;334334- }335335- snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);336336- subs->interface = fmt->iface;337337- subs->altset_idx = fmt->altset_idx;338338- }339339-340340- /* create a data pipe */341341- ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;342342- if (is_playback)343343- subs->datapipe = usb_sndisocpipe(dev, ep);344344- else345345- subs->datapipe = usb_rcvisocpipe(dev, ep);346346- subs->datainterval = fmt->datainterval;347347- subs->syncpipe = subs->syncinterval = 0;348348- subs->maxpacksize = fmt->maxpacksize;349349- subs->syncmaxsize = 0;350350- subs->fill_max = 0;235235+ subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,236236+ alts, fmt->endpoint, subs->direction,237237+ SND_USB_ENDPOINT_TYPE_DATA);238238+ if (!subs->data_endpoint)239239+ return -EINVAL;351240352241 /* we need a sync pipe in async OUT or adaptive IN mode */353242 /* check the number of EP, since some devices have broken···326273 * assume it as adaptive-out or sync-in.327274 */328275 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;276276+277277+ switch (subs->stream->chip->usb_id) {278278+ case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */279279+ case USB_ID(0x0763, 0x2081):280280+ if (is_playback) {281281+ implicit_fb = 1;282282+ ep = 0x81;283283+ iface = usb_ifnum_to_if(dev, 2);284284+285285+ if (!iface || iface->num_altsetting == 0)286286+ return -EINVAL;287287+288288+ alts = &iface->altsetting[1];289289+ goto add_sync_ep;290290+ }291291+ }292292+329293 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||330330- (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&294294+ (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&331295 altsd->bNumEndpoints >= 2) {332296 /* check sync-pipe endpoint */333297 /* ... and check descriptor size before accessing bSynchAddress···352282 the audio fields in the endpoint descriptors */353283 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||354284 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&355355- get_endpoint(alts, 1)->bSynchAddress != 0)) {285285+ get_endpoint(alts, 1)->bSynchAddress != 0 &&286286+ !implicit_fb)) {356287 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",357288 dev->devnum, fmt->iface, fmt->altsetting);358289 return -EINVAL;···361290 ep = get_endpoint(alts, 1)->bEndpointAddress;362291 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&363292 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||364364- (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {293293+ (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)) ||294294+ ( is_playback && !implicit_fb))) {365295 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",366296 dev->devnum, fmt->iface, fmt->altsetting);367297 return -EINVAL;368298 }369369- ep &= USB_ENDPOINT_NUMBER_MASK;370370- if (is_playback)371371- subs->syncpipe = usb_rcvisocpipe(dev, ep);372372- else373373- subs->syncpipe = usb_sndisocpipe(dev, ep);374374- if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&375375- get_endpoint(alts, 1)->bRefresh >= 1 &&376376- get_endpoint(alts, 1)->bRefresh <= 9)377377- subs->syncinterval = get_endpoint(alts, 1)->bRefresh;378378- else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)379379- subs->syncinterval = 1;380380- else if (get_endpoint(alts, 1)->bInterval >= 1 &&381381- get_endpoint(alts, 1)->bInterval <= 16)382382- subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;383383- else384384- subs->syncinterval = 3;385385- subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);386386- }387299388388- /* always fill max packet size */389389- if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)390390- subs->fill_max = 1;300300+ implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)301301+ == USB_ENDPOINT_USAGE_IMPLICIT_FB;302302+303303+add_sync_ep:304304+ subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,305305+ alts, ep, !subs->direction,306306+ implicit_fb ?307307+ SND_USB_ENDPOINT_TYPE_DATA :308308+ SND_USB_ENDPOINT_TYPE_SYNC);309309+ if (!subs->sync_endpoint)310310+ return -EINVAL;311311+312312+ subs->data_endpoint->sync_master = subs->sync_endpoint;313313+ }391314392315 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)393316 return err;···455390 if (changed) {456391 mutex_lock(&subs->stream->chip->shutdown_mutex);457392 /* format changed */458458- snd_usb_release_substream_urbs(subs, 0);459459- /* influenced: period_bytes, channels, rate, format, */460460- ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),461461- params_rate(hw_params),462462- snd_pcm_format_physical_width(params_format(hw_params)) *463463- params_channels(hw_params));393393+ stop_endpoints(subs, 0, 0, 0);394394+ deactivate_endpoints(subs);395395+396396+ ret = activate_endpoints(subs);397397+ if (ret < 0)398398+ goto unlock;399399+400400+ ret = snd_usb_endpoint_set_params(subs->data_endpoint, hw_params, fmt,401401+ subs->sync_endpoint);402402+ if (ret < 0)403403+ goto unlock;404404+405405+ if (subs->sync_endpoint)406406+ ret = snd_usb_endpoint_set_params(subs->sync_endpoint,407407+ hw_params, fmt, NULL);408408+unlock:464409 mutex_unlock(&subs->stream->chip->shutdown_mutex);410410+ }411411+412412+ if (ret == 0) {413413+ subs->interface = fmt->iface;414414+ subs->altset_idx = fmt->altset_idx;465415 }466416467417 return ret;···495415 subs->cur_rate = 0;496416 subs->period_bytes = 0;497417 mutex_lock(&subs->stream->chip->shutdown_mutex);498498- snd_usb_release_substream_urbs(subs, 0);418418+ stop_endpoints(subs, 0, 1, 1);499419 mutex_unlock(&subs->stream->chip->shutdown_mutex);500420 return snd_pcm_lib_free_vmalloc_buffer(substream);501421}···515435 return -ENXIO;516436 }517437438438+ if (snd_BUG_ON(!subs->data_endpoint))439439+ return -EIO;440440+518441 /* some unit conversions in runtime */519519- subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);520520- subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);442442+ subs->data_endpoint->maxframesize =443443+ bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);444444+ subs->data_endpoint->curframesize =445445+ bytes_to_frames(runtime, subs->data_endpoint->curpacksize);521446522447 /* reset the pointer */523448 subs->hwptr_done = 0;524449 subs->transfer_done = 0;525525- subs->phase = 0;526450 subs->last_delay = 0;527451 subs->last_frame_number = 0;528452 runtime->delay = 0;529453530530- return snd_usb_substream_prepare(subs, runtime);454454+ /* for playback, submit the URBs now; otherwise, the first hwptr_done455455+ * updates for all URBs would happen at the same time when starting */456456+ if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)457457+ return start_endpoints(subs);458458+459459+ return 0;531460}532461533462static struct snd_pcm_hardware snd_usb_hardware =···931842932843static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)933844{845845+ int ret;934846 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);935847 struct snd_usb_substream *subs = &as->substream[direction];936848937937- if (!as->chip->shutdown && subs->interface >= 0) {938938- usb_set_interface(subs->dev, subs->interface, 0);939939- subs->interface = -1;940940- }849849+ stop_endpoints(subs, 0, 0, 0);850850+ ret = deactivate_endpoints(subs);941851 subs->pcm_substream = NULL;942852 snd_usb_autosuspend(subs->stream->chip);943943- return 0;853853+854854+ return ret;855855+}856856+857857+/* Since a URB can handle only a single linear buffer, we must use double858858+ * buffering when the data to be transferred overflows the buffer boundary.859859+ * To avoid inconsistencies when updating hwptr_done, we use double buffering860860+ * for all URBs.861861+ */862862+static void retire_capture_urb(struct snd_usb_substream *subs,863863+ struct urb *urb)864864+{865865+ struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;866866+ unsigned int stride, frames, bytes, oldptr;867867+ int i, period_elapsed = 0;868868+ unsigned long flags;869869+ unsigned char *cp;870870+871871+ stride = runtime->frame_bits >> 3;872872+873873+ for (i = 0; i < urb->number_of_packets; i++) {874874+ cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;875875+ if (urb->iso_frame_desc[i].status && printk_ratelimit()) {876876+ snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);877877+ // continue;878878+ }879879+ bytes = urb->iso_frame_desc[i].actual_length;880880+ frames = bytes / stride;881881+ if (!subs->txfr_quirk)882882+ bytes = frames * stride;883883+ if (bytes % (runtime->sample_bits >> 3) != 0) {884884+#ifdef CONFIG_SND_DEBUG_VERBOSE885885+ int oldbytes = bytes;886886+#endif887887+ bytes = frames * stride;888888+ snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",889889+ oldbytes, bytes);890890+ }891891+ /* update the current pointer */892892+ spin_lock_irqsave(&subs->lock, flags);893893+ oldptr = subs->hwptr_done;894894+ subs->hwptr_done += bytes;895895+ if (subs->hwptr_done >= runtime->buffer_size * stride)896896+ subs->hwptr_done -= runtime->buffer_size * stride;897897+ frames = (bytes + (oldptr % stride)) / stride;898898+ subs->transfer_done += frames;899899+ if (subs->transfer_done >= runtime->period_size) {900900+ subs->transfer_done -= runtime->period_size;901901+ period_elapsed = 1;902902+ }903903+ spin_unlock_irqrestore(&subs->lock, flags);904904+ /* copy a data chunk */905905+ if (oldptr + bytes > runtime->buffer_size * stride) {906906+ unsigned int bytes1 =907907+ runtime->buffer_size * stride - oldptr;908908+ memcpy(runtime->dma_area + oldptr, cp, bytes1);909909+ memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);910910+ } else {911911+ memcpy(runtime->dma_area + oldptr, cp, bytes);912912+ }913913+ }914914+915915+ if (period_elapsed)916916+ snd_pcm_period_elapsed(subs->pcm_substream);917917+}918918+919919+static void prepare_playback_urb(struct snd_usb_substream *subs,920920+ struct urb *urb)921921+{922922+ struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;923923+ struct snd_urb_ctx *ctx = urb->context;924924+ unsigned int counts, frames, bytes;925925+ int i, stride, period_elapsed = 0;926926+ unsigned long flags;927927+928928+ stride = runtime->frame_bits >> 3;929929+930930+ frames = 0;931931+ urb->number_of_packets = 0;932932+ spin_lock_irqsave(&subs->lock, flags);933933+ for (i = 0; i < ctx->packets; i++) {934934+ counts = ctx->packet_size[i];935935+ /* set up descriptor */936936+ urb->iso_frame_desc[i].offset = frames * stride;937937+ urb->iso_frame_desc[i].length = counts * stride;938938+ frames += counts;939939+ urb->number_of_packets++;940940+ subs->transfer_done += counts;941941+ if (subs->transfer_done >= runtime->period_size) {942942+ subs->transfer_done -= runtime->period_size;943943+ period_elapsed = 1;944944+ if (subs->fmt_type == UAC_FORMAT_TYPE_II) {945945+ if (subs->transfer_done > 0) {946946+ /* FIXME: fill-max mode is not947947+ * supported yet */948948+ frames -= subs->transfer_done;949949+ counts -= subs->transfer_done;950950+ urb->iso_frame_desc[i].length =951951+ counts * stride;952952+ subs->transfer_done = 0;953953+ }954954+ i++;955955+ if (i < ctx->packets) {956956+ /* add a transfer delimiter */957957+ urb->iso_frame_desc[i].offset =958958+ frames * stride;959959+ urb->iso_frame_desc[i].length = 0;960960+ urb->number_of_packets++;961961+ }962962+ break;963963+ }964964+ }965965+ if (period_elapsed &&966966+ !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */967967+ break;968968+ }969969+ bytes = frames * stride;970970+ if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {971971+ /* err, the transferred area goes over buffer boundary. */972972+ unsigned int bytes1 =973973+ runtime->buffer_size * stride - subs->hwptr_done;974974+ memcpy(urb->transfer_buffer,975975+ runtime->dma_area + subs->hwptr_done, bytes1);976976+ memcpy(urb->transfer_buffer + bytes1,977977+ runtime->dma_area, bytes - bytes1);978978+ } else {979979+ memcpy(urb->transfer_buffer,980980+ runtime->dma_area + subs->hwptr_done, bytes);981981+ }982982+ subs->hwptr_done += bytes;983983+ if (subs->hwptr_done >= runtime->buffer_size * stride)984984+ subs->hwptr_done -= runtime->buffer_size * stride;985985+ runtime->delay += frames;986986+ spin_unlock_irqrestore(&subs->lock, flags);987987+ urb->transfer_buffer_length = bytes;988988+ if (period_elapsed)989989+ snd_pcm_period_elapsed(subs->pcm_substream);990990+}991991+992992+/*993993+ * process after playback data complete994994+ * - decrease the delay count again995995+ */996996+static void retire_playback_urb(struct snd_usb_substream *subs,997997+ struct urb *urb)998998+{999999+ unsigned long flags;10001000+ struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;10011001+ int stride = runtime->frame_bits >> 3;10021002+ int processed = urb->transfer_buffer_length / stride;10031003+10041004+ spin_lock_irqsave(&subs->lock, flags);10051005+ if (processed > runtime->delay)10061006+ runtime->delay = 0;10071007+ else10081008+ runtime->delay -= processed;10091009+ spin_unlock_irqrestore(&subs->lock, flags);9441010}94510119461012static int snd_usb_playback_open(struct snd_pcm_substream *substream)···1116872static int snd_usb_capture_close(struct snd_pcm_substream *substream)1117873{1118874 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);875875+}876876+877877+static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,878878+ int cmd)879879+{880880+ struct snd_usb_substream *subs = substream->runtime->private_data;881881+882882+ switch (cmd) {883883+ case SNDRV_PCM_TRIGGER_START:884884+ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:885885+ subs->data_endpoint->prepare_data_urb = prepare_playback_urb;886886+ subs->data_endpoint->retire_data_urb = retire_playback_urb;887887+ subs->running = 1;888888+ return 0;889889+ case SNDRV_PCM_TRIGGER_STOP:890890+ stop_endpoints(subs, 0, 0, 0);891891+ subs->running = 0;892892+ return 0;893893+ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:894894+ subs->data_endpoint->prepare_data_urb = NULL;895895+ subs->data_endpoint->retire_data_urb = NULL;896896+ subs->running = 0;897897+ return 0;898898+ }899899+900900+ return -EINVAL;901901+}902902+903903+int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)904904+{905905+ int err;906906+ struct snd_usb_substream *subs = substream->runtime->private_data;907907+908908+ switch (cmd) {909909+ case SNDRV_PCM_TRIGGER_START:910910+ err = start_endpoints(subs);911911+ if (err < 0)912912+ return err;913913+914914+ subs->data_endpoint->retire_data_urb = retire_capture_urb;915915+ subs->running = 1;916916+ return 0;917917+ case SNDRV_PCM_TRIGGER_STOP:918918+ stop_endpoints(subs, 0, 0, 0);919919+ subs->running = 0;920920+ return 0;921921+ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:922922+ subs->data_endpoint->retire_data_urb = NULL;923923+ subs->running = 0;924924+ return 0;925925+ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:926926+ subs->data_endpoint->retire_data_urb = retire_capture_urb;927927+ subs->running = 1;928928+ return 0;929929+ }930930+931931+ return -EINVAL;1119932}11209331121934static struct snd_pcm_ops snd_usb_playback_ops = {